I have this grep command to find files with matching pattern.
grep -oiE 'pattern1|pattern2|pattern3|pattern4' *pattern_in_a_filename* | sort -u
Output:
file_one:pattern1
file_two:pattern2
file_two:pattern3
What I want is to output the pattern4
saying that is not being found like this example:
file_one:pattern1
file_two:pattern2
file_two:pattern3
pattern4 not found
I'm doing thousands of thousands of patterns and it has to be done as quick as possible because these are crucial data needed in our operations.
Best Answer
Here's an
sh
script that produces the results you need.In this script, I assume you output the results of your
grep
to the filefound.txt
, and that you store your patterns in the file/path/to/foundFiles.txt
.As you can see, the
grep
in the loop will produce the same contents of the filefound.txt
while adding"$pattern" not found
for the missing ones.I also devised a second approach to your case:
In this case,
patterns.txt
needs to be already sorted forcomm
to work.The
comm
command compares the two files returning the lines present only inpatterns.txt
(-23
parameter), which is the list of patterns not found bygrep
.Then,
xargs
grabs every line (-L 1
) and echoes the line ({}
) with " not found" appended to it. The result ofxargs
is redirected to thenotFoundFiles.txt
file.Finally, you simply concatenate foundFiles.txt and notFoundFiles.txt into
finalList.txt
.