Garreau\, Alexandre
2018-03-13 15:49:47 UTC
Hi,
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),
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â (the variable
number of interleaved lines is what makes this problem beyond the scope
of paste from coreutils).
As the problem interested me, I generalized it into âinterleaving
arbitrary random interval of lines of an arbitrary number of filesâ,
with the option of filtering some input with sort -R at the end. I could
have put all the contents in a variable but I thought it wouldnât end
simple and readable, I didnât want to open any file several times. Hence
I started learning named streams (also process substitution but I didnât
found this useful), since, even thought I was going to work on an
arbitrary number of (thus numbered but not named) files, I saw in bash
manual that behavior with fd superior to 9 was unspecified, and I wanted
something robust yet able to operate on more than 9 files.
The interface I came with is (with later option to specify randomness):
interleave [<file> <interval-begin>-<interval-end>] ...
I ended with this code whose the unreadability introduced by the correct
opening and naming of each stream doesnât satisfy me as a proof of
having learning and mastered well enough named streams (I donât like
âparse all arguments then only then do actual stuffâ). Iâm especially
dissatisfied with the complexity of usage of my array (that I use to
number the streams (which are in arbitrary number)) as a consequence of
the arguments of my function being interleaved (I also thought of maybe
complexifying my interface to something like â[<file>:<interval>]...â in
order to unify and simplify retrieval of correct arguments):
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),
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â (the variable
number of interleaved lines is what makes this problem beyond the scope
of paste from coreutils).
As the problem interested me, I generalized it into âinterleaving
arbitrary random interval of lines of an arbitrary number of filesâ,
with the option of filtering some input with sort -R at the end. I could
have put all the contents in a variable but I thought it wouldnât end
simple and readable, I didnât want to open any file several times. Hence
I started learning named streams (also process substitution but I didnât
found this useful), since, even thought I was going to work on an
arbitrary number of (thus numbered but not named) files, I saw in bash
manual that behavior with fd superior to 9 was unspecified, and I wanted
something robust yet able to operate on more than 9 files.
The interface I came with is (with later option to specify randomness):
interleave [<file> <interval-begin>-<interval-end>] ...
I ended with this code whose the unreadability introduced by the correct
opening and naming of each stream doesnât satisfy me as a proof of
having learning and mastered well enough named streams (I donât like
âparse all arguments then only then do actual stuffâ). Iâm especially
dissatisfied with the complexity of usage of my array (that I use to
number the streams (which are in arbitrary number)) as a consequence of
the arguments of my function being interleaved (I also thought of maybe
complexifying my interface to something like â[<file>:<interval>]...â in
order to unify and simplify retrieval of correct arguments):