grep
grep
: Command-line utility (filter) for searching plaintext data for lines that match a regular expression.
ed
command g/re/p (globally search for a regular expression and print matching lines)grep
and egrep
.Tangent:
grep
variants
egrep
variant supports extended regular expression syntax.
- Added by Alfred Aho after Ken Thompson’s original implementation.
fgrep
variant searches for any list offixed
strings using the Aho-Corasick string matching algorithm.
Note: Utilities to use regular expressions are usually filters.
grep
Options-e pattern
: Provide a pattern-i
: Ignore uppercase vs. lowercase.-v
: Invert match.-c
: Output count of matching lines only.-l
: Output matching files only.-n
: Precede each matching line with a line number.-h
: Output matching lines without preceding them by file names.-s
: Suppress error messages about nonexistent or unreadable files.-f file
: Take regexes from a file.-o
: Output the matched parts of a matching line.grep
$ cat datafile.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
$ grep "NW" datafile.txt
northwest NW Charles Main 3.0 .98 3 34
$ grep "^north" datafile.txt
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
$ grep "3$" datafile.txt
western WE Sharon Gray 5.3 .97 5 23
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
$ grep "Charles Main" datafile.txt
northwest NW Charles Main 3.0 .98 3 34
$ grep "5\.." datafile.txt
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
$ grep "^[we]" datafile.txt
western WE Sharon Gray 5.3 .97 5 23
eastern EA TB Savage 4.4 .84 5 20
$ grep "[^0-9]" datafile.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
$ grep "[A-Z][A-Z] [A-Z]" datafile.txt
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
$ grep "ss* " datafile.txt
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
$ grep "[a-z]\{9\}" datafile.txt
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
northeast NE AM Main Jr. 5.1 .94 3 13
$ grep "\(3\)\.[0-9].*\1 *\1" datafile.txt
$ grep "\(3\)\.[0-9].*\1" datafile.txt
northwest NW Charles Main 3.0 .98 3 34
$ grep "\<north" datafile.txt
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
$ grep "\<north\>" datafile.txt
north NO Margot Weber 4.5 .89 5 9
$ grep "\<[a-z].*n\>" datafile.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13