Home > OS >  How to search for multiple patterns in multiple files using find or sed
How to search for multiple patterns in multiple files using find or sed

Time:06-16

grep -Zrl '$pattern1' /path/of/file | xargs -0 grep -rlZ '$pattern2' | xargs -0 grep -l '$pattern3' | xargs grep --color -C1 -E "$pattern1|$pattern2|$pattern3"

how to write the above command using sed or find.

The above command is basically searching 3 patterns at same time in multiple files.

CodePudding user response:

sed is Stream EDitor, might not be the best utility to use for searching patterns.

I'm guessing you're trying to grep 3 patterns in one set of files, which you already did in your last pipe:

grep --color -C1 -E "$pattern1|$pattern2|$pattern3"

I'd use find and grep together when I know there are some patterns in the filenames, then grep based on the results like:

find -iname '*pattern_in_filename*' -exec grep -E "$pattern1|$pattern2|$pattern3" {} ;

  • Related