How to Search for Strings, Patterns or Words From a File?

Use grep.

Some examples:

Searching for multiple patterns:

$ grep 'pattern1\|pattern2' <filename/filepath>

Using -E to treat the pattern as an extended regular expression:

Originally:

$ egrep 'pattern1|pattern2' <filename/filepath>

Now:

$ grep -E 'pattern1|pattern2' <filename/filepath>

The main difference between grep and grep -E is it supports additional meta characters, such as parentheses (), curly brackets {}, and question mark. The pipe character | is also treated as a meta character in extended grep.

Commonly used flags:

-r: recursive, search files and directories
-w: search for exact matches
-n: show line number
-i: ignore case
-c: only give the count of selected lines

comments powered by Disqus