Linux Combine two files by column

前端 未结 4 1400
闹比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: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
    

提交回复
热议问题