2つのファイルを比較し、一致するものを出力します

2つのファイルを比較し、一致するものを出力します

次のようなファイルがあります:

ファイル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

最初の列で 2 つのファイルを比較し、一致する部分を出力します。上記の内容の場合、出力は次のようになります。

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

関連情報