grep

This page provides an incomplete crib sheet for use of the grep command. For full details see the man page. All examples are based on the files shown at the top of this page.

cat filename

pattern1pattern2pattern3,pattern4pattern4,pattern4pattern5,pattern5,pattern4pattern6.pattern5pattern7pattern8pattern9pattern10Pattern1Pattern2Pattern3,pattern4Pattern4,pattern4Pattern5,pattern5,pattern4Pattern6,pattern5Pattern7Pattern8Pattern9Pattern10

cat filename2

pattern1pattern2

grep 'pattern1' filename

pattern1pattern10

Match words not substrings...

grep -w 'pattern1' filename

grep --word-regexp 'pattern1' filename

pattern1

Ignore case...

grep -i 'pattern1' filename

grep --ignore-case 'pattern1' filename

pattern1pattern10Pattern1Pattern10

OR

grep 'pattern1\|pattern2' filename

grep -E 'pattern1|pattern2' filename

grep --extended-regexp 'pattern1|pattern2' filename

grep -F -f filename2 filename

grep --fixed-strings --file=filename2 filename

pattern1pattern2pattern10

AND

grep -E 'Pattern.*pattern4' filename

grep --extended-regexp 'Pattern.*pattern4' filename

Pattern3,pattern4Pattern4,pattern4Pattern5,pattern5,pattern4

NOT

grep -v 'pattern' filename

grep --invert-match 'pattern' filename

Pattern1Pattern2Pattern7Pattern8Pattern9Pattern10