2013-07-10 35 views
0

我想比較兩個文件file1 $ 1等於file2 $ 1並顯示輸出file1 $ 1 $ 2 $ 3 $ 4 $ 5 $ file2 $ 2 $ 5。並將file1 $ 5的差異 - 文件2 $ 5加入第一個字段的2個文件

輸入文件1.txt的

1,raja,AP,NIND,14:51:56.46 
2,mona,KR,SIND,12:41:46.36 
3,JO,TM,SIND,18:31:56.36 
4,andrew,sind,13:43:23.12 
5,drew,sind,17:53:53.42 

輸入文件2.txt

5,raju,UP,NIND,11:51:56.46 
6,NAG,KR,SIND,12:41:46.36 
7,JO,TM,SIND,18:31:56.36 
8,andrew,sind,kkd,14:43:23.12 
4,andrew,sind,ggf,15:53:53.42 
10,asJO,TM,SIND,16:31:56.36 
3,sandrew,sind,gba,9:43:23.12 
2,xcandrew,sind,sds,6:53:53.42 
1,cv,GTM,SIND,5:31:56.36 
9,mnJO,TM,SIND,2:20:56.36 

輸出:

1,raja,AP,NIND,14:51:56.46,cv,5:31:56.36 
2,mona,KR,SIND,12:41:46.36,xcandrew,6:53:53.42 
3,JO,TM,SIND,18:31:56.36,sandrew,9:43:23.12 
4,andrew,sind,13:43:23.12,andrew,15:53:53.42 
5,drew,sind,17:53:53.42,raju,11:51:56.46 

回答

2

隨着awk你會怎麼做:

$ awk 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1],$2,$5}' FS=, OFS=, f1 f2 
5,drew,sind,17:53:53.42,raju,11:51:56.46 
4,andrew,sind,13:43:23.12,andrew, 
3,JO,TM,SIND,18:31:56.36,sandrew, 
2,mona,KR,SIND,12:41:46.36,xcandrew, 
1,raja,AP,NIND,14:51:56.46,cv,5:31:56.36 

如果你想然後通過管道排序,以sort輸出:

$ awk 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1],$2,$5}' FS=, OFS=, f1 f2 | sort 
1,raja,AP,NIND,14:51:56.46,cv,5:31:56.36 
2,mona,KR,SIND,12:41:46.36,xcandrew, 
3,JO,TM,SIND,18:31:56.36,sandrew, 
4,andrew,sind,13:43:23.12,andrew, 
5,drew,sind,17:53:53.42,raju,11:51:56.46 

使用替代join

$ join -j1 -t, -o 1.1,1.2,1.3,1.4,1.5,2.2,2.5 <(sort f1) <(sort f2) 
1,raja,AP,NIND,14:51:56.46,cv,5:31:56.36 
2,mona,KR,SIND,12:41:46.36,xcandrew, 
3,JO,TM,SIND,18:31:56.36,sandrew, 
4,andrew,sind,13:43:23.12,,andrew, 
5,drew,sind,17:53:53.42,,raju,11:51:56.46 
相關問題