awk를 사용하여 다중 열 파일 비교 및 ​​출력 출력

awk를 사용하여 다중 열 파일 비교 및 ​​출력 출력

파일 1:

cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 283 F30109_THK_T0_CFPUR0701_DS005 2022-06-02 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 284 F30109_THK_T0_CFPUR0701_DS005 2022-06-03 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 285 F30109_THK_T0_CFPUR0701_DS005 2022-06-04 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 286 F30109_THK_T0_CFPUR0701_DS005 2022-06-05 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 287 F30109_THK_T0_CFPUR0701_DS005 2022-06-06 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 288 F30109_THK_T0_CFPUR0701_DS005 2022-06-07 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS006 hostgroups-1d1w 282 F30109_THK_T0_CFPUR0701_DS006 2022-06-01 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 283 F30109_THK_T0_CFPUR0702_DS006 2022-06-02 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 284 F30109_THK_T0_CFPUR0702_DS006 2022-06-03 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 285 F30109_THK_T0_CFPUR0702_DS006 2022-06-04 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 286 F30109_THK_T0_CFPUR0702_DS006 2022-06-05 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 287 F30109_THK_T0_CFPUR0702_DS006 2022-06-06 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS006 hostgroups-1d1w 288 F30109_THK_T0_CFPUR0703_DS006 2022-06-07 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS007 hostgroups-1d1w 282 F30109_THK_T0_CFPUR0703_DS007 2022-06-01 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS007 hostgroups-1d1w 283 F30109_THK_T0_CFPUR0703_DS007 2022-06-02 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS007 hostgroups-1d1w 284 F30109_THK_T0_CFPUR0703_DS007 2022-06-03 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS007 hostgroups-1d1w 285 F30109_THK_T0_CFPUR0703_DS007 2022-06-04 00:00:00

파일 2:

cfpur0701 hostgroups-1d1w 2022-06-02 00:00:00 2022-06-09 00:00:00
cfpur0701 hostgroups-1d1w 2022-06-03 00:00:00 2022-06-10 00:00:00
cfpur0701 hostgroups-1d1w 2022-06-04 00:00:00 2022-06-11 00:00:00
cfpur0701 hostgroups-1d1w 2022-06-05 00:00:00 2022-06-12 00:00:00
cfpur0701 hostgroups-1d1w 2022-06-06 00:00:00 2022-06-13 00:00:00
cfpur0701 hostgroups-1d1w 2022-06-07 00:00:00 2022-06-14 00:00:00
cfpur0701 hostgroups-1d1w 2022-06-01 00:00:00 2022-06-08 00:00:00
cfpur0702 hostgroups-1d1w 2022-06-02 00:00:00 2022-06-09 00:00:00
cfpur0702 hostgroups-1d1w 2022-06-03 00:00:00 2022-06-10 00:00:00
cfpur0702 hostgroups-1d1w 2022-06-04 00:00:00 2022-06-11 00:00:00
cfpur0702 hostgroups-1d1w 2022-06-05 00:00:00 2022-06-12 00:00:00
cfpur0702 hostgroups-1d1w 2022-06-06 00:00:00 2022-06-13 00:00:00
cfpur0702 hostgroups-1d1w 2022-06-07 00:00:00 2022-06-14 00:00:00
cfpur0703 hostgroups-1d1w 2022-06-01 00:00:00 2022-06-08 00:00:00
cfpur0703 hostgroups-1d1w 2022-06-02 00:00:00 2022-06-09 00:00:00
cfpur0703 hostgroups-1d1w 2022-06-03 00:00:00 2022-06-10 00:00:00
cfpur0703 hostgroups-1d1w 2022-06-04 00:00:00 2022-06-11 00:00:00
cfpur0703 hostgroups-1d1w 2022-06-05 00:00:00 2022-06-12 00:00:00
cfpur0703 hostgroups-1d1w 2022-06-06 00:00:00 2022-06-13 00:00:00
cfpur0703 hostgroups-1d1w 2022-06-07 00:00:00 2022-06-14 00:00:00
cfpur0801 hostgroups-1d1w 2022-06-01 00:00:00 2022-06-08 00:00:00
cfpur0801 hostgroups-1d1w 2022-06-02 00:00:00 2022-06-09 00:00:00
cfpur0801 hostgroups-1d1w 2022-06-03 00:00:00 2022-06-10 00:00:00
cfpur0801 hostgroups-1d1w 2022-06-04 00:00:00 2022-06-11 00:00:00

원하는 출력:

cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 283 F30109_THK_T0_CFPUR0701_DS005 2022-06-02 00:00:00 2022-06-09 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 284 F30109_THK_T0_CFPUR0701_DS005 2022-06-03 00:00:00 2022-06-10 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 285 F30109_THK_T0_CFPUR0701_DS005 2022-06-04 00:00:00 2022-06-11 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 286 F30109_THK_T0_CFPUR0701_DS005 2022-06-05 00:00:00 2022-06-12 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 287 F30109_THK_T0_CFPUR0701_DS005 2022-06-06 00:00:00 2022-06-13 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS005 hostgroups-1d1w 288 F30109_THK_T0_CFPUR0701_DS005 2022-06-07 00:00:00 2022-06-14 00:00:00
cfpur0701 Pgroup F30109 cf3010922 F30109_FPUR0701_PRD03E_DS006 hostgroups-1d1w 282 F30109_THK_T0_CFPUR0701_DS006 2022-06-01 00:00:00 2022-06-08 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 283 F30109_THK_T0_CFPUR0702_DS006 2022-06-02 00:00:00 2022-06-09 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 284 F30109_THK_T0_CFPUR0702_DS006 2022-06-03 00:00:00 2022-06-10 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 285 F30109_THK_T0_CFPUR0702_DS006 2022-06-04 00:00:00 2022-06-11 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 286 F30109_THK_T0_CFPUR0702_DS006 2022-06-05 00:00:00 2022-06-12 00:00:00
cfpur0702 Pgroup F30109 cf3010922 F30109_FPUR0702_PRD03E_DS006 hostgroups-1d1w 287 F30109_THK_T0_CFPUR0702_DS006 2022-06-06 00:00:00 2022-06-13 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS006 hostgroups-1d1w 288 F30109_THK_T0_CFPUR0703_DS006 2022-06-07 00:00:00 2022-06-14 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS007 hostgroups-1d1w 282 F30109_THK_T0_CFPUR0703_DS007 2022-06-01 00:00:00 2022-06-08 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS007 hostgroups-1d1w 283 F30109_THK_T0_CFPUR0703_DS007 2022-06-02 00:00:00 2022-06-09 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS007 hostgroups-1d1w 284 F30109_THK_T0_CFPUR0703_DS007 2022-06-03 00:00:00 2022-06-10 00:00:00
cfpur0703 Pgroup F30109 cf3010922 F30109_FPUR0703_PRD03E_DS007 hostgroups-1d1w 285 F30109_THK_T0_CFPUR0703_DS007 2022-06-04 00:00:00 2022-06-11 00:00:00

file1의 줄 수는 항상 file2의 줄 수보다 큽니다. 다음을 수행해야 합니다.

파일 1과 파일 2의 열 1을 비교하고 일치하는 경우 파일 1의 열 6을 파일 2의 열 2와 비교하고 마지막으로 파일 1의 열 9를 파일 2의 열 2와 비교합니다. 비교를 위해 열 3. 세 가지 조건이 모두 충족되면 file2에서 타임스탬프(열 5 및 6)를 가져와서 file1에 추가하여 출력 파일을 생성합니다.

여러 버전의 awk를 시도했지만 NR==FNR많은 진전이 없었습니다.

답변1

awk 'FNR==NR{map[$1,$2,$3,$4] = $5 FS $6; next}
     ($1,$6,$9,$10) in map {print $0,map[$1,$6,$9,$10]}' file2 file1

(file1의 마지막 두 열과 일치시키기 위해) 필요하다고 생각했기 때문에 매핑에 날짜 및 시간 필드를 사용했습니다.

첫 번째 파일 패스( FNR==NR)에서는 공통 배열에 추가할 필드(마지막 2개)를 저장하고 처음 4개는 해시로 사용됩니다. next첫 번째 파일의 모든 줄에 대해 나머지 코드를 실행하지 않는 것이 필요합니다.

두 번째 파일의 경우 배열 값을 추가하고 선택한 필드가 기존 배열 해시를 형성하는 경우에만 줄을 인쇄합니다.

관련 정보