help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] multiline random paste or how to properly use (named) st


From: Garreau\, Alexandre
Subject: Re: [Help-bash] multiline random paste or how to properly use (named) streams
Date: Tue, 13 Mar 2018 20:04:34 +0100
User-agent: Gnus (5.13), GNU Emacs 25.1.1 (x86_64-pc-linux-gnu)

On 2018-03-13 at 12:57, Greg Wooledge wrote:
> On Tue, Mar 13, 2018 at 04:49:47PM +0100, Garreau, Alexandre wrote:

>> in a way that each (at random) 1 to 5 lines of “foo” would be surrounded
>> by (at random) 2 to 4 (preferably but not necessarily) random unique
>> (not appearing several times in total) lines of “bar”
>
> When you say "surrounded by", do you actually mean "followed by"?

> 2) The first file is to be used in sequential order.  The second file is
>    to be randomly shuffled.

Originally he said “so that each line of foo is between two random lines
of bar”, actually by “two random line” he just clarified me he meant
“two sets of a random number of lines”, but actually he just said me he
wanted them ordered, not shuffled :/ sorry

>    2a) A recent GNU coreutils is available for the shuffling.

Oh indeed, just found it :) it seems neat, but I actually didn’t search
as I had the strong impression sort -R does the same… doesn’t it?

> So, as I understand it, the problem is as follows:
>
> 1) We have two text files containing multiple lines.  There is no semantic
>    meaning to the lines (they are filenames, but we're not going to be
>    opening or statting the files).  They are of roughly equal sizes.

Not of equal sizes.

> 3) A single output file is to be produced, containing lines from both
>    input files, following a sequence of steps.  At each step, read 1-5
>    lines from the first file, and write them to the output.  Then read
>    2-4 lines from the (shuffled) second file, and write those to the
>    output.
>
> If this is the full problem spec, then here's one approach:

Ideally I wanted to work on a more general problem, as I specified with
the interleave function: to work with parametrable intervals and an
arbitrary number of files.

> while true; do
>     # Read 1-5 lines from first file.
>     n=$((1 + RANDOM%5))
>     for ((i=1; i<=n; i++)); do
>       IFS= read -r line <&3 || break
>       printf %s\\n "$line"
>     done
>
>     # Read 2-4 lines from second file.
>     n=$((2 + RANDOM%3))
>     for ((i=1; i<=n; i++)); do
>       IFS= read -r line <&4 || break
>       printf %s\\n "$line"
>     done
> done

Oh I didn’t think of using break… that’s more imperative but that
certainly avoid using cycling variables that can confuse… yet I don’t
know if that would really simplify in the case of an arbitrary number of
files to interleave… I don’t like having magic numbers and filenames in
programs :)

> This terminates as soon as it reaches EOF in either file.  This may
> cause some lines from the *other* file (whichever one didn't hit EOF)
> to be unused.  If that's a problem, well, you can fix it. ;-)

In my version it terminates when everything has been read, and then it
ends reading the rest of the files.  He just clarified me that he
apparently doesn’t just want plain fixed random-numbers but stretch the
intervals so that the lines are scatered evenly (with some randomness I
think) accross the final output so that one file doesn’t end after the
other… I’m not precisely sure of the exact behavior he wants but I’ll
know later.

>> Recently a friend of mine asked me if I would have a right away solution
>> to interleaves line of a file foo and a file bar (m3u files actually),
>
> Music play lists.  Good.  Having the context is helpful.  An m3u file
> has one filename per line (either CR-LF or newline terminated), as I
> understand it.  No blank lines, no markup or groupings or comments or
> anything else.

Initially my parenthesis stating the context was longer than the rest of
the paragraph and I poundered several minutes whether it was worth
keeping it or not: happy to notice my (as you can see evidently terribly
insufficent) synthesis skills have improved :)

On 2018-03-13 at 13:47, Greg Wooledge wrote:
> On Tue, Mar 13, 2018 at 12:57:45PM -0400, Greg Wooledge wrote:
>> while true; do
>>     # Read 1-5 lines from first file.
>>     n=$((1 + RANDOM%5))
>>     for ((i=1; i<=n; i++)); do
>>      IFS= read -r line <&3 || break
>
> Both of these "break" commands should have been "break 2".  Sorry 'bout
> that.  Also, the script as written is completely untested, so there
> could be other bugs lurking.

ack :) what do you mean by unnested? it’s indented as I see it…



reply via email to

[Prev in Thread] Current Thread [Next in Thread]