...

Part of Linux Commands Cheat Sheet

Search lines that contains words in a file


grep text filename

# -i for case insensitive
grep -i text filename

Search from output (pipe)


cat filename | grep text

Count result lines


grep -c text filename

Print line numbers


grep -n text filename

Print context


# 3 lines before and after each matching line
grep -C 3 text filename

# 3 lines after each matching line
grep -B 3 text filename

# 3 lines after each matching line
grep -A 3 text filename

Search directory


grep -r text directory

# find files containing text
# -l for list filenames
grep -rl text directory

And, or and not


# and
grep -E 'a.*b|b.*a' filename
grep a filename | grep b

# or
grep 'a\|b' filename
grep -e 'a\|b' filename
grep -E 'a|b' filename
grep -e a -e b filename

# not
grep -v text filename

References

How To Use grep Command In Linux / UNIX - nixCraft

How do I find all files containing specific text on Linux? - Stack Overflow

7 Linux Grep OR, Grep AND, Grep NOT Operator Examples

wbswjc-blog