<mohammadrony>

grep

Text Select

grep 'foo' file.txt
cat file.txt | grep 'bar'

Print matched text only

echo 'bar baz' | grep -o 'bar'

Ignore case of matched text

cat file.txt | grep -i 'foo'

Print previous few lines of matched line

cat file.txt | grep -B 3 'foo'

Print next few lines of matched line

cat file.txt | grep -A 3 'foo'

Regex

Print lines doesn't start with #

grep -v '^#' foo.txt

Print line container number starting with 1

grep '1[0-9]*' foo.txt

Print only numbers

grep -oP '\d+' foo.txt

Print files containing text

grep -n 'text' *.txt

Examples

grep -Rnw '/path/to/somewhere/' -e 'pattern'
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
grep --exclude=\*.o -rnw '/path/to/search/' -e "pattern"
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"