-1

I have files similar to this:

File1:

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

File2:

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

I want to compare the two files on the first column and output where they match. For the content above, the output would be:

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

How would I do this?

Also how would I reverse it so I can show those that have no match? Comparing File1 to File2 and vice versa?

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

If you want to do a simple test for non-matching entries in 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

and conversely non-matching entries in 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

(the print is implicit).

  • Thanks. Is there an easy way to reverse it so I can show those that have no match? Comparing File1 to File2 and vice versa? – Proletariat Jul 6 '17 at 12:25
  • @Proletariat please see augmented answer – steeldriver Jul 6 '17 at 12:58
0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.