bash merge files by matching columns

后端 未结 1 1330
情深已故
情深已故 2021-01-03 13:36

I do have two files:

 File1

 12    abc
 34    cde
 42    dfg
 11    df
 9     e   


 File2

 23    abc
 24    gjr
 12    dfg
 8     df

I

相关标签:
1条回答
  • 2021-01-03 14:24

    Since I was testing with bash 3.2 running as sh (which does not have process substitution as sh), I used two temporary files to get the data ready for use with join:

    $ sort -k2b File2 > f2.sort
    $ sort -k2b File1 > f1.sort
    $ cat f1.sort
    12    abc
    34    cde
    11    df
    42    dfg
    9     e  
    $ cat f2.sort
    23    abc
    8     df
    12    dfg
    24    gjr
    $ join -1 2 -2 2 -o 1.1,2.1,0 -a 1 -a 2 -e NA f1.sort f2.sort
    12 23 abc
    34 NA cde
    11 8 df
    42 12 dfg
    9 NA e
    NA 24 gjr
    $
    

    With process substitution, you could write:

    join -1 2 -2 2 -o 1.1,2.1,0 -a 1 -a 2 -e NA <(sort -k2b File1) <(sort -k2b File2)
    

    If you want the data formatted differently, use awk to post-process the output:

    $ join -1 2 -2 2 -o 1.1,2.1,0 -a 1 -a 2 -e NA f1.sort f2.sort |
    > awk '{ printf "%-5s %-5s %s\n", $1, $2, $3 }'
    12    23    abc
    34    NA    cde
    11    8     df
    42    12    dfg
    9     NA    e
    NA    24    gjr
    $
    
    0 讨论(0)
提交回复
热议问题