I have 2 text files, file1 and file2. I need to output those lines in file1 that do not exist in file2.
The lines in both files are not in the same order and there are additional lines in file2, which are irrelevant.
For example:
$ grep testme file1
chmod -f 644 /root/testme
chown -h root:root /root/testme
$ grep testme file2
chmod -f 777 /root/testme
chown -h dude:dude /root/testme
The above grep
is an example of 2 lines in file1 that are different, or as such do not exist in file2. Both files are about 5 MB.
Using the following procedure in Linux Bash (RHEL 7)
while read -r line; do
[ "$(\grep "^$line\$" file2 )" ] || echo $line
done < file1
displays...
chmod -f 644 /root/testme
chown -h root:root /root/testme
grep: Unmatched [ or [^`
... and won't finish. It also takes a very long time.
I tried another approach, which is very fast, but only prints 1 of the 2 lines:
$ fgrep -v -f file2 file1
chmod -f 644 /root/testme
$
Why is it only showing 1 line?
The 2 lines are somewhere in the middle of the file, terminated by a CR.
Any ideas please? Thanks!