Hello! GREP

We'll cover the following

What is GREP? #

The command grep is a small utility for searching plain-text data sets for lines matching a regular expression. Its name comes from the globally search a regular expression and print.

A simple example of a common usage of grep is the following, which searches the file colors.txt for lines containing the text string red:

$ grep "red" colors.txt

The v option reverses the sense of the match and prints all lines that do not contain blue, as in this example.

$ grep -v "blue" colors.txt

The i option in grep helps to match words that are case insensitive, as shown in below example.

$ grep -i "bLuE" colors.txt

The n option identifies the lines where matches occurred:

$ grep -n "orange" colors.txt
4: Orange color
6: Ornage company

GREP and regular expressions #

While grep supports a handful of regular expression commands, it does not support certain useful sequences such as the + and ? operators. If you would like to use these, you will have to use extended grep (egrep).

The Following command illustrates the ?, which matches 1 or 0 occurences of the previous character w:

$ grep  "yellow?" colors.txt

egrep example:

$ egrep  "red|yellow" colors.txt

Note that grep does not do the pipe (|), which funcitons as an "OR" in the expression.

Get hands-on with 1200+ tech skills courses.