grep

grep: Command-line utility (filter) for searching plaintext data for lines that match a regular expression.

Tangent: grep variants

Note: Utilities to use regular expressions are usually filters.

Examples: Using 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