0

I want to make something like multiplication:

File1:

aa
bb

File2:

cc
dd

File3:

eee
fff
ggg

I want a result like:

aa cc eee
aa cc fff
aa cc ggg
bb dd eee
bb dd fff
bb dd ggg

File1 & File2 first element will multiply every element of File3, and same as second element of File1 & File2 multiply with every element of File3.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
TonyStark
  • 379
  • 6
  • 22
  • 1. Why? / 2. Do you have to use `bash`? No Python? / 3. Basically you are looking for [permutations](http://stackoverflow.com/q/3846123/1525423) – Finwood Apr 04 '16 at 16:55
  • @Finwood im making little bot with bash and some binaries, so i have to go through bash... – TonyStark Apr 04 '16 at 17:00

2 Answers2

6

This would work:

$ join -j 9999 <(paste file1 file2) file3
 aa cc eee
 aa cc fff
 aa cc ggg
 bb dd eee
 bb dd fff
 bb dd ggg

It joins on a non-existing field (field 9999), which creates the Cartesian product of the input files. For the input files, paste file1 file2 combines the first two files into one, and join uses process substitution.

A slight snag is that there is a space introduced on each line; to get rid of that, you can pipe to sed:

join -j 9999 <(paste file1 file2) file3 | sed 's/^ //'

or specify an output format:

join -j 9999 -o 1.1,1.2,2.1 <(paste file1 file2) file3
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
2

You could use a nested for loop.

for ab in $(paste -d ' ' File1 File2); do
  for c in $(cat File3); do
    echo "$ab $c"
  done
done

It doesn’t scale, obviously, but it may be enough for your use case.

Benjamin Barenblat
  • 1,311
  • 6
  • 19