Grep

regular expressions

There are short forms of typical forms.

 \d for [0-9]
 \D for [^0-9]
 \w for [a-zA-Z_0-9]
 \W for [^a-zA-Z_0-9]
 \s for [\ \f\n\r\t\v]
which means whitespace characters
 \S for [^\ \f\n\r\t\v]

 ?
 *
 +
 {min,}
 {min,max}
 {n}
 (asdf|ASDF) 
alternatives

Special characters:

 ^ begin of line
 $ end of line
 \b empty string at the beginning or the end of a word
 \B negation of \b
 \< empty string at the beginning of the word
 \> empty string at the end of the word

grep, egrep, fgrep

 egrep
(extended grep) knows more regular expressions than grep
 fgrep
fixed or fast grep because it knows less regular expressions than grep and therefore it is much faster than grep. This is very useful in large files.
 rgrep
recursive grep searchs subfolders

Examples:

 grep '197.' 
returns lines with 197 and a arbitrary character after this pattern
 grep '^[AS]'
lines which begin with A or S
 grep '^[^AS]' 
lines which begin not with A or S
 grep '[a-z]\{13\}'
lines with thirteen lower letters
 grep '\<War' 
lines where words begin with War like Warm Warning or War
 grep 'A\>'
lines where words end with A
 grep '\<thing\>'
lines where thing stands alone and not something nor anything
 grep '\<[G].*ain
lines with begin of a word with G and then arbitrary characters until pattern ain
 grep -c 'pattern'
counts how often the pattern pattern occurs
 grep -i 
do not differ between lower and capital letters (ignore case)
 grep -q
returns 0 if pattern has been found else 1
 grep -s 
no error messages if a file does not exist
 grep -v
lines where the given pattern does not occur
egrep
 egrep 'pattern1|pattern2'
lines with pattern1 or pattern2