我有类似这样的文件:
文件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