Bash join command Bash join command unix unix

Bash join command


Works for me (almost). You should specify -t $'\t' for the tab character, not just -t \t. Bash does not interpret \t unless in $'' quotes.

join -t $'\t' -o 1.2,2.2 <(echo  $'27\t2728\t2229\t3730\t1531\t2132\t1333\t1834\t24' | sort) <(echo $'27\t728\t1329\t630\t1231\t3032\t533\t1034\t28' | sort)27      722      1337      615      1221      3013      518      1024      28


First sort both files. Then use join to join on the first field of both files. You also need to pipe the output through sed if you want to remove the space and thus convert a a into aa. This is shown below:

$ join -t " " -1 1 -2 1 -a 1 -a 2  <(sort file1) <(sort file2) | sed 's/ \([a-z]\) / \1/g'1 aa2 b3 c4 d5 e6 ff7 g8 h


this should work for your both cases:

awk 'NR==FNR{a[$1]=$2;next;} {a[$1]=($1 in a)?a[$1]$2:$2}END{for(x in a)print x,a[x]}' f1 f2|sort

output for case one:

1 aa2 b3 c4 d5 e6 ff7 g8 h

output for case two:

27 27728 221329 37630 151231 213032 13533 181034 2428