Grep

Grep is your tool of choice to search through file in Linux.

Inputs

There is 3 main way to feed inputs in grep:

File

grep search file

Dir (recursive)

grep -r search ./dir

Pipe

cat file | grep search

Options

Case insensitive

grep -i search file

grep -v search file

grep -w search file

grep -x search file

grep -e search1 -e search2 file

Display line number

grep -n search file

Display line After/Before/(C)Both

grep -A1 search file

grep -B1 search file

grep -C1 search file

Max number of line

grep -m1 search file

Count

grep -c search file

REGular EXpressions

grep -E search file

example: grep -E t$ file (search for lines ending in "t")

Force color

useful when you pass grep to something else before printing.

grep --color=always

Only color

Will print everything but color your search pattern.

grep "search\|$" file

The search text "search\|$" is actually a trick, it will match lines that have pattern OR lines that have an end. Because all lines have an end, all lines are matched, but the end of a line isn't actually any characters, so it won't be colored.[1]

Sources

1]: [bash - Colorized grep -- viewing the entire file with highlighted matches - Stack Overflow