Linux Combine two files by column

前端 未结 4 1387
闹比i
闹比i 2020-12-14 23:28

I am trying to combine two files as below (Intersection)

ID     Name  Telephone       
1      John     011
2      Sam      013
3      Jena     014
4      Pet         


        
相关标签:
4条回答
  • 2020-12-15 00:15

    It is far easier to use the join command:

    $ cat a.txt 
    ID     Name  Telephone       
    1      John     011
    2      Sam      013
    3      Jena     014
    4      Peter    015
    $ cat b.txt 
    ID     Remark1  Remark2       
    1       Test1    Test2
    2       Test3    Test4
    3       Test5    Test6
    4       Test7    Test8
    5       Test7    Test8
    6       Test7    Test8
    7       Test7    Test8
    8       Test7    Test8
    9       Test7    Test8
    $ join a.txt b.txt 
    ID Name Telephone Remark1 Remark2
    1 John 011 Test1 Test2
    2 Sam 013 Test3 Test4
    3 Jena 014 Test5 Test6
    4 Peter 015 Test7 Test8
    

    Use the column command to pretty print it:

    $ join a.txt b.txt | column -t
    ID  Name   Telephone  Remark1  Remark2
    1   John   011        Test1    Test2
    2   Sam    013        Test3    Test4
    3   Jena   014        Test5    Test6
    4   Peter  015        Test7    Test8
    
    0 讨论(0)
  • 2020-12-15 00:18
    $ awk -v OFS='\t' '
    NR==1   { print $0, "Remark1", "Remark2"; next }
    NR==FNR { a[$1]=$0; next }
    $1 in a { print a[$1], $2, $3 }
    ' Test1.txt Test2.txt
    ID     Name  Telephone  Remark1 Remark2
    1      John     011     Test1   Test2
    2      Sam      013     Test3   Test4
    3      Jena     014     Test5   Test6
    4      Peter    015     Test7   Test8
    
    0 讨论(0)
  • 2020-12-15 00:24
     awk -F"\t" '
         {key = $1 FS $2 FS $3 FS $4}
         NR == 1 {header = key}
         !(key in result) {result[key] = $0; next}
         { for (i=5; i <= NF; i++) result[key] = result[key] FS $i }
         END {
             print result[header]
             delete result[header]
             PROCINFO["sorted_in"] = "@ind_str_asc"    # if using GNU awk
             for (key in result) print result[key]
         }
     ' Test1.txt Test2.txt ... > result.txt
    
    0 讨论(0)
  • 2020-12-15 00:27

    Another alternative would be pr which is used for formating files to print.

    $ pr -tm -w 50 Test1.txt Test2.txt
    ID     Name  Telephone   ID Remark1  Remark2
    1      John      011     1   Test1    Test2
    2      Sam       013     2   Test3    Test4
    3      Jena      014     3   Test5    Test6
    4      Peter     015     4   Test7    Test8
                             5   Test7    Test8
                             6   Test7    Test8
                             7   Test7    Test8
                             8   Test7    Test8
                             9   Test7    Test8
    

    The most important is the m flag which merges files into columns. The t flag removes headers and footers - since we're not going to print on paper, we don't need them. The last w flag is for setting width.

    0 讨论(0)
提交回复
热议问题