I have the following script:
#!/bin/bash
result=$(grep "pattern 1\|pattern 2\|pattern 3\|pattern 4\|pattern 5\|pattern 6\|pattern 7\|pattern 8" file_data.dat)
if [ -n "$result" ]; then
printf '%s\n' "$result"
else
printf 'No match found for pattern "%s"\n' "$pattern"
fi
It works but it has an issue, if there is no match for one of the patterns, I want to know which pattern did not match.
I also tried with:
#!/bin/bash
if
grep "pattern 1\|pattern 2\|pattern 3\|pattern 4\|pattern 5\|pattern 6\|pattern 7\|pattern 8" file_data.dat
then
echo "All patterns found"
else
echo "Missing pattern"
fi
But it has same issue.
grepruns over the file (once) and prints all lines that match at least one of the patterns, so the output is only empty (or the exit code not 0) if none of the patterns match. Perhaps you rather want to loop over the patterns and check them separately?