![awk를 사용하여 파일 1과 파일 2의 서로 다른 열을 일치시키는 방법은 무엇입니까?](https://linux55.com/image/40757/awk%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%ED%8C%8C%EC%9D%BC%201%EA%B3%BC%20%ED%8C%8C%EC%9D%BC%202%EC%9D%98%20%EC%84%9C%EB%A1%9C%20%EB%8B%A4%EB%A5%B8%20%EC%97%B4%EC%9D%84%20%EC%9D%BC%EC%B9%98%EC%8B%9C%ED%82%A4%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
다음 두 파일이 있습니다.
첫 번째 파일은 다음과 같습니다.
3184 2014-07-28 04:15 global.Remote-Access 10.111.8.25 81.245.6.25 tcp
3268
3035 2014-07-28 04:16 global.Remote-Access 10.111.8.12 81.245.6.25 tcp
3268
두 번째 파일은 다음과 같습니다.
1 Jul 28 04:12 2014-07-28 id967254(group3)[attribute1 attribute2]
Tunneling: User with IP 10.111.8.12 10 connected
1 Jul 28 04:15 2014-07-28 id920767(group2)[attribute3 attribute4 ....
attribute n] Tunneling: User with IP 10.111.8.25 connected
1 Jul 28 04:16 2014-07-28 ID926072(group3)[attribute1 attribute2]
Tunneling:User with IP 10.111.8.12 connected
파일 1의 원본 IP 주소가 파일 2와 같고, 파일 1의 시간( hh:mm
)과 날짜( ) yyyy-mm-dd
가 파일 2와 같은 경우 세 번째 파일은 다음과 같습니다.
3184 04:15 2014-07-28 global.Remote-Access id920767(group2)[attribute3
attribute4 .... attribute n] 10.111.8.25 81.245.6.25 tcp 3268
3035 04:16 2014-07-28 global.Remote-Access ID926072(group3)[attribute1
attribute2] 10.111.8.12 81.245.6.25 tcp 3268
이를 달성하려면 어떻게 해야 합니까 awk
?
답변1
이 시도:
$ awk 'FNR==NR{a[$2$3$5];next} ($5$4$(NF-1)) in a' file1 file2
1 Jul 28 04:15 2014-07-28 id920767(group2)[attribute3 attribute4 .... attribute n] Tunneling: User with IP 10.111.8.25 connected
1 Jul 28 04:16 2014-07-28 ID926072(group3)[attribute1 attribute2] Tunneling:User with IP 10.111.8.12 connected
답변2
awk 'NR == FNR {t=($4 $5 $(NF-1));
$1=$2=$3=$4=$5=X;
$0=$0; $1=$1;
sub(/].*$/, "]");
a[t] = $0; next}
($3 $2 $5) in a {$4 = ($4 " " a[$3 $2 $5])}1' file2 file1