Using GNU awk
where it does support specific length(array)
feature (and some other awk
implementation which may can support) and not required if files are sorted.
gawk 'FNR==NR{seen[$0];next} ($0 in seen){delete seen[$0]};
END{print (!length(seen))?"Matched":"Not Matched"}' file2 file1
This is reading file2 into an array called seen
with the key as entire line of file2.
Then read file1 and for each line if matched with lines in array seen then delete that key.
At the end if the array was empty means all lines in file2 exist in file1 and will print Matched
, otherwise will display Not Matched
.
For the compatibility in all awk
implementations.
awk 'FNR==NR{seen[$0];next} ($0 in seen){delete seen[$0]};
END{for(x in seen);print (!x)?"Matched":"Not Matched"}' file2 file1
To ignoring empty lines/or lines with whitespaces only if in file2, you would need to add NF
to the condition in NR==FNR && NF {...
to skip reading them into the array.
file2
contains 2 linesA
, do you needfile1
to contain at least 2 linesA
?