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暗黙的です)。

おすすめ記事