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

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

提交回复
热议问题