Join two files on Linux

前端 未结 5 1229
予麋鹿
予麋鹿 2021-01-15 21:51

I have two files; I want to join them.

$cat t1
 1 1.2
 2 2.2
$cat t2
 1
 2
 1

I want to have the output below

$cat joind.tx         


        
5条回答
  •  隐瞒了意图╮
    2021-01-15 22:41

    join requires that both files to be sorted. If you sort them first, you'll get all your output

    $ sort t1 > t1.sorted
    $ sort t2 > t2.sorted
    $ join -j1 -o 1.1,1.2 t1.sorted t2.sorted
    1 1.2
    1 1.2
    2 2.2
    

    Without the sort:

    $ join -j1 -o 1.1,1.2 t1 t2
    1 1.2
    2 2.2
    

    This assumes that the order of your inputs don't need to be preserved; if they do, you will need a custom script like other answers have provided.

提交回复
热议问题