0

File 1:

1F
2F
3F
4F
5f

File 2:

1F
2F
3F
4F
5f

I have a simple code that produces all possible combinations

#!/bin/bash
    for a in $(awk '{print $1}' intf1) 
    do 
        for b in $(awk '{print $1}' intf2) 
        do 
            echo -e "$a:$b" >> file
        done 
    done

Output of this code:

1F:1F
1F:2F
1F:3F
1F:4F
2F:1F
etc

But I would like to:

1) Completely avoid repetitions

2) "Select the number" (the number of words (lines) which he will be taken from the second file):

Each two lines in second file:

1F:2F
1F:3F
2F:3F
2F:4F
3F:4F
3F:5F
4F:5F

Each three lines in second file:

 1F:2F
 1F:3F
 1F:4F
 2F:3F
 2F:4F
 2F:5F
etc..

And etc

SysRq308
  • 125
  • 4

1 Answers1

0

If the files could be sorted (and exclude repeating lines), this could work:

printf "%s\n" $(eval "echo {$(sort -u file.txt | paste -sd, -)}:{$(sort -u file2.txt | head -2 | paste -sd, -)}") | sort -u

It uses the bash expansion to generate the combinations, like:

$ echo {a,b}{X,Y,Z}
aX aY aZ bX bY bZ

also, you must ABSOLUTELY trust the content of the files, because the code uses dangerous eval.

The argument to head like head -2 could be used for limiting the cound of lines from the file2.txt

The code produces (by limiting the second file to 2 already sorted lines) the following:

20160702F:20160702F
20160702F:20160714F
20160714F:20160702F
20160714F:20160714F
20160807F:20160702F
20160807F:20160714F
20160819F:20160702F
20160819F:20160714F
20160831F:20160702F
20160831F:20160714F
20160912F:20160702F
20160912F:20160714F
clt60
  • 62,119
  • 17
  • 107
  • 194