Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

bash - Linux Combine two files by column

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

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

Second file Test2.txt

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

Then Final result

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

I did like this as below,

awk -F"" '
    {key = $1 }
    NR == 1 {header = key}
    !(key in result) {result[key] = $0; next}
    { for (i=2; i <= NF; i++) result[key] = result[key] FS $i }
    END {
        print result[header]
        delete result[header]
        PROCINFO["sorted_in"] = "@ind_str_asc"    
        for (key in result) print result[key]
    }
' Test1.txt Test2.txt > result.txt

And I just notice that this is Union set. Including all data Test1 and Test2.

I would like to show only for Intersection case as what I expected result. (1, 2 ,3 ,4) only

Do you guys have any idea? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...