How to print the inputted pattern which don’t have matching lines

greptext processing

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.

#!/bin/sh

grep -f /path/to/patterns.txt /path/to/*_856_2017* | sort -u > /path/to/foundFiles.txt 

while read -r LINE
do
    grep -F "$LINE" /path/to/foundFiles.txt
    if [ $? -eq 1 ]
    then
        echo "$LINE" not found
    fi
done < /path/to/patterns.txt

In this script, I assume you output the results of your grep to the file found.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 file found.txt while adding "$pattern" not found for the missing ones.

I also devised a second approach to your case:

#!/bin/sh

grep -f /path/to/patterns.txt /path/to/*_856_2017* |
    sort -u > /path/to/foundFiles.txt

comm -23 /path/to/patterns.txt /path/to/foundFiles.txt |
    xargs -L 1 -I {} echo {} not found > /path/to/notFoundFiles.txt

cat /path/to/foundFiles.txt /path/to/notFoundFiles.txt > /path/to/finalList.txt

In this case, patterns.txt needs to be already sorted for comm to work.

The comm command compares the two files returning the lines present only in patterns.txt (-23 parameter), which is the list of patterns not found by grep.

Then, xargs grabs every line (-L 1) and echoes the line ({}) with " not found" appended to it. The result of xargs is redirected to the notFoundFiles.txt file.

Finally, you simply concatenate foundFiles.txt and notFoundFiles.txt into finalList.txt.

Related Question