Bash join command

后端 未结 3 1424
长发绾君心
长发绾君心 2020-12-16 19:13

Infile1:

1 a
3 c
4 d
6 f

Infile2:

1 a 
2 b
5 e
6 f
7 g
8 h

How do I join these files with the unix join c

3条回答
  •  天命终不由人
    2020-12-16 19:32

    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 aa
    2 b
    3 c
    4 d
    5 e
    6 ff
    7 g
    8 h
    

    output for case two:

    27 277
    28 2213
    29 376
    30 1512
    31 2130
    32 135
    33 1810
    34 2428
    

提交回复
热议问题