Grep – How to Identify Patterns That Aren’t Matched

grep

I'm looking for a command or script to do the following – given:

file1.txt:

abcd
efgh 
ijkl
mnop

file2.txt:

123abcd123
123efgh123
123mnop123

I want a command that does something like this:

ungrep file1.txt file2.txt

and returns the following:

ijkl

In other words it is giving me the lines in file1.txt that will not return any results on a grep of file2.txt. I know that I can do this by iterating through file1.txt, grepping file2.txt for each line and storing the result, and outputting any lines where the result is empty, but I was hoping for a more efficient way to do this.

Best Answer

With GNU grep the following should work. Using the -f option, pass file1.txt as a "pattern file" - but also pass it in a second time as a data file. Use -o to report only the matching parts. Finally extracts those words that match only once - these correspond to the lines from file1.txt that do not find a match in file2.txt.

grep -h -o -f  file1.txt file2.txt file1.txt | sort | uniq -u
ijkl
Related Question