Hi.
Apologies for the lengthy post.
This is probably not an improvement as much as it is a meta-answer. This is how we went about solving problems like this by generalizing.
I'll assume that you could get the contents of the directories by looking at the content, and one such command might be:
ls -1 A > data1
and so on for B, C, etc.
We then would use a local code gather
to obtain at least one line from each of the files data1, data2, etc.
The companion program is scatter
.
As I have noted before we have not yet decided to publish our codes, but when we post meta-solutions like this, we can post the documentation. Then folks can decide whether this is a reasonable approach for them to pursue.
Here is a demonstration that exercises code gather
:
#!/usr/bin/env bash
# @(#) s1 Demonstrate intersperse, shuffle of lines, gather.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C gather dixf scatter
FILE=${1-data1}
pl " Input data files data*:"
head data*
pl " Results, 1 item from each list:"
gather data*
pl " Results, 2 from column A, one from B and C:"
gather data1:2 data2 data3
pl " Results, as previous, but separator is newline:"
gather -s '\n' data1:2 data2 data3
pl " Help from gather:"
gather -h
pl " Details about gather, scatter:"
dixf gather scatter
exit 0
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.8 (jessie)
bash GNU bash 4.3.30
gather (local) 1.1
dixf (local) 1.49
scatter (local) 1.4
-----
Input data files data*:
==> data1 <==
1.txt
2.txt
3.txt
4.txt
5.txt
==> data2 <==
a.txt
b.txt
c.txt
d.txt
e.txt
==> data3 <==
101.txt
102.txt
103.txt
104.txt
105.txt
-----
Results, 1 item from each list:
1.txt
a.txt
101.txt
2.txt
b.txt
102.txt
3.txt
c.txt
103.txt
4.txt
d.txt
104.txt
5.txt
e.txt
105.txt
-----
Results, 2 from column A, one from B and C:
1.txt 2.txt
a.txt
101.txt
3.txt 4.txt
b.txt
102.txt
5.txt 103.txt
c.txt
104.txt
d.txt
105.txt
e.txt
-----
Results, as previous, but separator is newline:
1.txt
2.txt
a.txt
101.txt
3.txt
4.txt
b.txt
102.txt
5.txt
103.txt
c.txt
104.txt
d.txt
105.txt
e.txt
-----
Help from gather:
gather - Read, shuffle, weave, intersperse lines from multiple inputs to STDOUT.
usage: gather [options] -- [files]
options:
--separator=SEP
Set output token separator to SEP, default " ", and "\n" is
accepted as NEWLINE. The SEP is used when more than one line is
read from a file. This allows one to easily capture short lines
into a single output line.
--help (or -h)
print this message and quit.
[files]
filename1:count1 filename2:count2 ... filename3:count3
Each filenamei will be read in succession, and will be written
to standard output, continuing until EOF on every file.
-----
Details about gather, scatter:
gather Read, shuffle, weave, intersperse lines from multiple input files. (what)
Path : ~/bin/gather
Version : 1.1
Length : 224 lines
Type : Perl script, ASCII text executable
Shebang : #!/usr/bin/env perl
Help : probably available with -h,--help
Modules : (for perl codes)
strict 1.08
warnings 1.23
English 1.09
Carp 1.3301
Getopt::Long 2.42
feature 1.36_01
scatter Write, deal, unravel disperse lines to multiple output files. (what)
Path : ~/bin/scatter
Version : 1.4
Length : 190 lines
Type : Perl script, ASCII text executable
Shebang : #!/usr/bin/env perl
Modules : (for perl codes)
strict 1.08
warnings 1.23
English 1.09
Carp 1.3301
Data::Dumper 2.151_01
Getopt::Long 2.42
feature 1.36_01
You can also, with the right shell, use embedded commands like this:
$ gather <( ls -1 A ) <( ls -1 B )
1.txt
a.txt
2.txt
b.txt
3.txt
c.txt
4.txt
d.txt
5.txt
e.txt
Best wishes ... cheers, drl