Unix join on multiple fields on two files [closed] Unix join on multiple fields on two files [closed] unix unix

Unix join on multiple fields on two files [closed]


awk -F\| 'NR == FNR {  f2[$1, $2] = $3 OFS $4  next  }($1, $2) in f2 {  print $0, f2[$1, $2]  }' OFS=\| test2.txt test1.txt


Hmm, that works for your example:

 sed 's/|/+/' t1.txt>$$.tmp;sed 's/|/+/' t2.txt|join -t \| -j 1 $$.tmp -|sed 's/+/|/';rm $$.tmp


This also seems to work:

$ sed 's/|/\t/2' 1.txt > 1_1.txt; sed 's/|/\t/2' 2.txt > 2_1.txt;$ join -j1 1_1.txt 2_1.txt | tr ' ' '|'$ rm 1_1.txt 2_1.txt

A one-liner without temporary file creation (thanks to @dbaupp):

$ join -j1 <(sed 's/|/\t/2' 1.txt) <(sed 's/|/\t/2' 2.txt) | tr ' ' '|'