
Tengo archivos similares a este:
Archivo1:
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
Archivo2:
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
Quiero comparar los dos archivos en la primera columna y mostrar donde coinciden. Para el contenido anterior, el resultado sería:
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
¿Cómo haría esto?
Además, ¿cómo lo revertiría para poder mostrar aquellos que no coinciden? ¿Comparando Archivo1 con Archivo2 y viceversa?
Respuesta1
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
Si desea realizar una prueba sencilla para detectar entradas que no coinciden en Archivo2
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
y, por el contrario, entradas que no coinciden en Archivo1
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
(el print
está implícito).
Respuesta2
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