2013-06-27 4 views
2

두 열의 파일을 결합하려고합니다. 출력은 두 번째 파일의 열과 첫 번째 파일의 일부 열이어야합니다.특정 열에 두 파일을 결합하는 AWK

예를 들어 나의 첫 번째 파일은 다음과 같습니다

subscriberid unsubscribetime unsubscribeip listid statid 탈퇴

내 두 번째 파일은 다음과 같습니다

listid statid listname시^Ownername OWNEREMAIL createdate subscriberid bouncetype bouncetime 경비원

첫 번째 파일에서 5 번째이고 두 번째 파일에서 두 번째 인 "statid"열에 이들을 조인해야합니다. 그런 다음 첫 번째 파일에서 두 번째, 세 번째 및 여섯 번째 열을 추가합니다. 출력은 다음과 같아야합니다

listid statid listname시^Ownername OWNEREMAIL createdate subscriberid bouncetype bouncetime bouncerule unsubscribetime unsubscribeip 탈퇴

내가 문제 구문을 이해하는 데,하지만이 명령을 사용했다 :

awk 'NR==FNR{a[$5]=$2;next} $2 in a{print $0, a[$5]}' file1 file2 

나는 "print $ 0"때문에 file2 컬럼을 줄 것이고 두번째 컬럼 폼 파일은 [$ 5] = $ 2이지만 file2 열만 출력합니다.

어디에서 실수합니까? file1에서 file2로 2, 3 및 6 열을 결합하는 방법은 무엇입니까?

모든 도움말/설명은 대단히 감사합니다! 이 예에서

두 파일은 하나 개의 행을 가지고

답변

2

나는 당신이보고 조금 당신이 그것을 알아낼거야 그것에 대해 생각하면 확신 :

$ awk 'NR==FNR{a[$5]=$2" "$3" "$6;next} $2 in a{print $0, a[$2]}' file1 file2 
listid statid listname ownername owneremail createdate subscriberid bouncetype bouncetime bouncerule unsubscribetime unsubscribeip unsubscribed 
+0

와우가 작동한다! 나는 어떻게 이해하려고 노력할 것이다 : D Thank you! – Dropout