
Tenho arquivos parecidos com este:
Arquivo1:
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
Arquivo2:
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
Quero comparar os dois arquivos na primeira coluna e gerar a saída onde eles correspondem. Para o conteúdo acima, a saída seria:
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
Como eu faria isso?
Além disso, como eu reverteria para poder mostrar aqueles que não têm correspondência? Comparando Arquivo1 com Arquivo2 e vice-versa?
Responder1
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
Se você quiser fazer um teste simples para entradas não correspondentes no Arquivo2
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
e, inversamente, entradas não correspondentes no Arquivo1
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
(o print
está implícito).
Responder2
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