Q1Which symbol matches the start of a line in a regular expression?
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.
^ is the line start, $ the line end, and . any one character.| Symbol | Meaning | Example | What the example means |
|---|---|---|---|
^ | Matches the start of a line | grep '^c' f | Find lines that start with c |
$ | Matches the end of a line | grep 't$' f | Find lines that end with t |
. | Matches any one character | grep 'c.t' f | Any one char between c and t (cat/cot) |
* | Zero or more of the preceding character | grep 'ab*c' f | Any number of b between a and c (ac/abc) |
[abc] | Matches any one of the characters | grep '[cb]at' f | Find cat or bat |
[a-z] | Any one character in the range | grep '[a-z]at' f | One lowercase char + at (cat/bat) |
\| | Matches either (or) | grep 'cat\|dog' f | Lines 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
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
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
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.
| Symbol | Meaning | Example | What the example means |
|---|---|---|---|
+ | One or more of the preceding | grep -E 'ab+c' f | One or more b between a and c (abc/abbc) |
| | Matches either (or) | grep -E 'cat|dog' f | Lines containing cat or dog |
() | Grouping | grep -E '(ab)+' f | One or more repetitions of ab (ab/abab) |
-E enables extended regex, and -o prints only the matched part.| How to write it | Meaning | Example |
|---|---|---|
grep -E | Enable extended regex (ERE) | grep -E 'cat|dog' f |
grep -o | Print only the matched part | grep -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
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.
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
Knowledge Check
Answer each question one by one.
Q2What does the * in grep 'ab*c' f represent?
Q3What is printed when you run grep -o cat line.txt?