0

E.g. the inputs are two sets A and B.

set A is stored in file a.txt as below:

apple
orange
grape
...

set B is stored in file b.txt as below:

tomato
potato
cucumber
...

The output is c.txt like:

apple    potato
orange   tomato
grape    celery
...      ...

Note the mapping between them is randomly generated. I.e., each time

map.sh a.txt b.txt > c.txt

usually give a different mapping.

Can this be implemented in shell (or awk, sed)?

JackWM
  • 10,085
  • 22
  • 65
  • 92

2 Answers2

3
paste <(shuf a.txt) <(shuf b.txt)

If you would like the first column to stay constant, you can simply supply a.txt as the first argument to paste:

paste a.txt <(shuf b.txt)
0

If you did want to do this in Awk, you could use rand(). Just make sure you set a new random seed (srand()) each time:

$ awk ' BEGIN { srand() }
    NR == FNR {
        a[rand(), NR] = $1; 
        next;
    } 
    1 == FNR { asorti(a, v) } 
    {
        i = length(v); 
        j = v[i];
        delete v[i]; 
        print $1, a[j];
    }
' a.txt b.txt

tomato orange
potato apple
cucumber grape

$ awk ' BEGIN { srand() }
    NR == FNR {
        a[rand(), NR] = $1; 
        next;
    } 
    1 == FNR { asorti(a, v) } 
    {
        i = length(v); 
        j = v[i];
        delete v[i]; 
        print $1, a[j];
    }
' a.txt b.txt

tomato apple
potato orange
cucumber grape