Learn by reading through in order

Basic Regular Expressions — grep Patterns

Practice grep's basic regular expressions — the 5 symbols ^/$/./*/[], extended regex with -E, and -o to print only the match — by extracting from text files.

A tool to test your regex

You can also try the patterns in this article in the browser-based Regex Tester. It shows where your pattern matches against your input text, right on the spot.

Basic Regular Expressions — ^ $ . * []

A regular expression is a way to describe a pattern in text. grep interprets the string you search for as a regular expression, so you can find lines by conditions like 'starts with this character' or 'any one character here', not just by a fixed string. The 5 symbols to learn for basic regular expressions are ^, $, ., *, and [].

^ and $ are anchors for the start and end of a line. . matches any one character, and * means zero or more repetitions of the preceding character. [abc] matches any one character inside the brackets, and [a-z] matches one lowercase character by range. Combine them for flexible searches.

Basic regex symbols and meanings
^ct$c.tlines starting with clines ending with tany one char between c and t
^ is the line start, $ the line end, and . any one character.
SymbolMeaningExampleWhat the example means
^Matches the start of a linegrep '^c' fFind lines that start with c
$Matches the end of a linegrep 't$' fFind lines that end with t
.Matches any one charactergrep 'c.t' fAny one char between c and t (cat/cot)
*Zero or more of the preceding charactergrep 'ab*c' fAny number of b between a and c (ac/abc)
[abc]Matches any one of the charactersgrep '[cb]at' fFind cat or bat
[a-z]Any one character in the rangegrep '[a-z]at' fOne lowercase char + at (cat/bat)
\|Matches either (or)grep 'cat\|dog' fLines containing cat or dog
echo -e 'cat\ncot\ndog' > words.txt   # create material
grep '^c' words.txt                       # cat and cot, which start with c
grep 't$' words.txt                       # cat and cot, which end with t
grep 'c.t' words.txt                      # cat and cot matching c?t

① Create a 3-line material file with echo -e 'cat\ncot\ndog' > words.txt.

② Pass grep a pattern with the start-of-line anchor and show only the lines that start with c.

③ Then, with a pattern using the end-of-line anchor, show the lines that end with t.

④ Finally, with a pattern that puts the any-one-character symbol in between, show the lines where there's one character between c and t. (Run it correctly and an explanation will appear.)

Linux console
0 / 4 completed
Loading Linux Terminal...

Repetition and Character Classes — * and []

* means zero or more repetitions of the preceding character. ab*c matches lines where there are zero or more b, like ac, abc, and abbc. [] matches any one character inside the brackets, so [cb]at picks up both cat and bat. Writing a range like [a-z] represents one lowercase character.

echo -e 'ac\nabc\nabbc\naxc' > rep.txt   # create material
grep 'ab*c' rep.txt                       # ac abc abbc, with zero or more b
echo -e 'cat\nbat\nrat' > cls.txt      # create material
grep '[cb]at' cls.txt                     # the 2 lines cat and bat

① Create material to check repetition with echo -e 'ac\nabc\nabbc\naxc' > rep.txt.

② Pass grep a pattern using the symbol for zero or more repetitions of the preceding character, and show the lines where there can be any number of b between a and c.

③ Create material to check character classes with echo -e 'cat\nbat\nrat' > cls.txt.

④ With a pattern using the bracket character class, show only cat and bat.

Linux console
0 / 4 completed
Loading Linux Terminal...

Or — Matching Either with \|

In basic regular expressions, | (or) is treated as an ordinary character as-is.

To express alternation, add a backslash and write \|, so grep 'cat\|dog' matches lines containing cat or dog.

With grep -E, which you'll learn later, you can write | directly instead of \|.

echo -e 'cat\ndog\nfox' > sel.txt   # create material
grep 'cat\|dog' sel.txt              # the 2 lines containing cat or dog

① Create a 3-line material file with echo -e 'cat\ndog\nfox' > sel.txt.

② To express 'or' in basic regular expressions, add a backslash to |. Pass grep the pattern 'cat\|dog' and show the lines containing cat or dog.

③ Confirm that fox isn't printed because it matches neither.

Linux console
0 / 2 completed
Loading Linux Terminal...

Extended Regex and Printing the Match — grep -E / grep -o

In basic regular expressions, + | ( ) need escaping, like \+ \| \( \), but with grep -E you can write them directly. grep -E enables extended regular expressions (ERE). You can now write + (one or more), | (or), and () (group) directly, which needed escaping in basic regex. grep -E 'cat|dog' matches lines containing cat or dog.

grep -o prints only the matched substring, not the whole matching line. If there are several matches on one line, it prints each on a separate line. It's handy when you want to pull just the matches out of a log and count them.

SymbolMeaningExampleWhat the example means
+One or more of the precedinggrep -E 'ab+c' fOne or more b between a and c (abc/abbc)
|Matches either (or)grep -E 'cat|dog' fLines containing cat or dog
()Groupinggrep -E '(ab)+' fOne or more repetitions of ab (ab/abab)
What grep -E and grep -o do
grep -E 'cat|dog' flines containing cat or doggrep -o 'cat' fprints only the matched cat
-E enables extended regex, and -o prints only the matched part.
How to write itMeaningExample
grep -EEnable extended regex (ERE)grep -E 'cat|dog' f
grep -oPrint only the matched partgrep -o 'cat' f

More advanced regex

grep -P (Perl-compatible regex) is a GNU grep extension. In this course you learn basic regular expressions (^ $ . * []), grep -E to enable extended regex, and grep -o to print only the match. These work the same way on any Linux.

echo -e 'cat\ndog\nfox' > animals.txt   # create material
grep -E 'cat|dog' animals.txt              # the 2 lines cat or dog
echo -e 'ac\nabc\nabbc' > plus.txt      # create material
grep -E 'ab+c' plus.txt                    # abc abbc, with one or more b
echo -e 'cat cat dog' > line.txt         # create material
grep -o cat line.txt                       # prints each matched cat on its own line

① Create a material file with echo -e 'cat\ndog\nfox' > animals.txt.

② Add the option that enables extended regex to grep and show the lines containing either cat or dog.

③ Create material with echo -e 'ac\nabc\nabbc' > plus.txt, and with the extended-regex grep -E 'ab+c', show the lines with one or more b.

④ Create material with several words on one line with echo -e 'cat cat dog' > line.txt.

⑤ Add the option that prints only the match to grep, search for cat, and confirm that each matched word comes out on its own line.

Linux console
0 / 6 completed
Loading Linux Terminal...

Filtering Command Output — | grep

grep can search not only a file given as an argument but also the output of another command passed through a pipe |.

Pipe the result of ls or cat into | grep to narrow it down to just the lines you need.

ls cmds | grep '^c' shows only the names that start with c from the listing of the cmds directory, and cat file | grep word filters a file's contents the same way.

Filtering command output with | grep
ls cmdsthe whole listing comes outls cmds | grep '^c'narrow to lines starting with c
Pipe a command's output to grep with | to narrow it to just the matching lines. cmds is a small material directory you create yourself.
mkdir -p cmds                                # small material directory
touch cmds/cat cmds/cut cmds/sort            # prepare 3 names
ls cmds | grep '^c'                          # cat and cut, which start with c
echo -e 'apple\nbanana\ncherry' > fruit.txt   # create material
cat fruit.txt | grep an                      # only banana, which contains an

① Prepare a small material directory with mkdir -p cmds and touch cmds/cat cmds/cut cmds/sort.

② Pipe the output of ls cmds into grep with | and show only the names that start with c.

③ Create 3-line material with echo -e 'apple\nbanana\ncherry' > fruit.txt, then pipe the output of cat fruit.txt into | grep and show only the lines containing an.

Linux console
0 / 5 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1Which symbol matches the start of a line in a regular expression?

Q2What does the * in grep 'ab*c' f represent?

Q3What is printed when you run grep -o cat line.txt?