Grep

Grep

Type of grep

grep = grep -G # Basic Regular Expression (BRE)
fgrep = grep -F # fixed text, ignoring meta-charachetrs
egrep = grep -E # Extended Regular Expression (ERE)
pgrep = grep -P # Perl Compatible Regular Expressions (PCRE)
rgrep = grep -r # recursive

Grep and count number of empty lines

grep -c "^$"

Grep and return only integer

grep -o '[0-9]*'
#or
grep -oP '\d'

Grep integer with certain number of digits (e.g. 3)

grep ‘[0-9]\{3\}’
# or
grep -E ‘[0-9]{3}’
# or
grep -P ‘\d{3}’

Grep only IP address

Grep whole word (e.g. 'target')

Grep returning lines before and after match (e.g. 'bbo')

Grep string starting with (e.g. 'S')

Extract text between words (e.g. w1,w2)

Grep lines without word (e.g. 'bbo')

Grep lines not begin with string (e.g. #)

Grep variables with space within it (e.g. myvar="some strings")

Grep only one/first match (e.g. 'bbo')

Grep and return number of matching line(e.g. 'bbo')

Count occurrence (e.g. three times a line count three times)

Case insensitive grep (e.g. 'bbo'/'BBO'/'Bbo')

COLOR the match (e.g. 'bbo')!

Grep search all files in a directory(e.g. 'bbo')

Search all files in directory, do not ouput the filenames (e.g. 'bbo')

Search all files in directory, output ONLY the filenames with matches(e.g. 'bbo')

Grep OR (e.g. A or B or C or D)

Grep AND (e.g. A and B)

Regex any single character (e.g. ACB or AEB)

Regex with or without a certain character (e.g. color or colour)

Grep all content of a fileA from fileB

Grep a tab

Grep variable from variable

Grep strings between a bracket()

Grep number of characters with known strings in between(e.g. AAEL000001-RA)

Skip directory (e.g. 'bbo')

Last updated

Was this helpful?