두 파일을 비교하고 일치하는 결과를 출력합니다.

두 파일을 비교하고 일치하는 결과를 출력합니다.

다음과 비슷한 파일이 있습니다.

파일1:

1.1.1.7 mounting of udf filesystems is disabled Fail
1.1.1.8 mounting of FAT filesystems is disabled  Fail
1.1.5 noexec option set on /tmp partition   Fail
1.1.17 noexec option set on /dev/shm partition  Fail
1.1.21 sticky bit is set on all world-writable directories  Fail
1.3.1 AIDE is installed Fail

파일2:

1.1.1.7 Ensure mounting of udf filesystems is disabled
1.1.1.8 Ensure mounting of FAT filesystems is disabled
1.1.3 Ensure nodev option set on /tmp partition
1.1.4 Ensure nosuid option set on /tmp partition

첫 번째 열의 두 파일을 비교하고 일치하는 위치를 출력하고 싶습니다. 위 콘텐츠의 경우 출력은 다음과 같습니다.

1.1.1.7 Ensure mounting of udf filesystems is disabled
1.1.1.7 mounting of udf filesystems is disabled Fail
1.1.1.8 Ensure mounting of FAT filesystems is disabled
1.1.1.8 mounting of FAT filesystems is disabled  Fail

어떻게 해야 할까요?

또한 일치하지 않는 항목을 표시할 수 있도록 어떻게 반전시킬 수 있나요? File1을 File2와 비교하거나 그 반대로 비교합니까?

답변1

awk '
  NR == FNR {a[$1] = $0; next} 
  ($1 in a) {print; print a[$1]}
' File1 File2
1.1.1.7 Ensure mounting of udf filesystems is disabled
1.1.1.7 mounting of udf filesystems is disabled Fail
1.1.1.8 Ensure mounting of FAT filesystems is disabled
1.1.1.8 mounting of FAT filesystems is disabled  Fail

File2에서 일치하지 않는 항목에 대해 간단한 테스트를 수행하려는 경우

awk 'NR==FNR {a[$1]=$0; next} !($1 in a)' File1 File2
1.1.3 Ensure nodev option set on /tmp partition
1.1.4 Ensure nosuid option set on /tmp partition

반대로 File1의 일치하지 않는 항목

awk 'NR==FNR {a[$1]=$0; next} !($1 in a)' File2 File1
1.1.5 noexec option set on /tmp partition   Fail
1.1.17 noexec option set on /dev/shm partition  Fail
1.1.21 sticky bit is set on all world-writable directories  Fail
1.3.1 AIDE is installed Fail

(암시 print적입니다).

답변2

perl -lane '
        @ARGV and $h{$F[0]}=$_,next;
        print "$_\n$h{$F[0]}" if exists $h{$F[0]};
' File2 File1

1.1.1.7 mounting of udf filesystems is disabled Fail
1.1.1.7 Ensure mounting of udf filesystems is disabled
1.1.1.8 mounting of FAT filesystems is disabled  Fail
1.1.1.8 Ensure mounting of FAT filesystems is disabled

관련 정보