2013-07-10 5 views
0

에 2 개 파일 가입 I는 file1 $ 1 file2 $ 1과 동일한 두 파일을 비교하고, 출력 file1 $ 1을, $ 2 $ 3 $ 4 $ 5 file2 $ 2 $ 5 표시 할 것이다. 그리고 파일 1 $ 5 차이 - 파일 2 $ (5)첫번째 필드

입력 파일 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 

출력 : 당신이 할 것 awk

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 '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