Chapter 3. Regular Expressions Primer
Regular expressions (regex) are a powerful method for describing a text pattern to be matched by various tools. There is only one place in bash where regular expressions are valid, using the =~ comparison in the [[ compound command, as in an if statement. However, regular expressions are a crucial part of the larger toolkit for commands like grep, awk, and sed in particular. They are powerful and thus worth knowing. Once you’ve mastered regular expressions, you’ll wonder how you ever got along without them.
For many of the examples in this chapter, we will be using the file frost.txt with its seven—yes seven—lines of text; see Example 3-1.
Example 3-1. frost.txt
1 Two roads diverged in a yellow wood, 2 And sorry I could not travel both 3 And be one traveler, long I stood 4 And looked down one as far as I could 5 To where it bent in the undergrowth; 6 7 Excerpt from The Road Not Taken by Robert Frost
The content of frost.txt will be used to demonstrate the power of regular expressions to process text data. This text was chosen because it requires no prior technical knowledge to understand.
Commands in Use
We introduce the grep family of commands to demonstrate the basic regex patterns.
grep
The grep command searches the content of the files for a given pattern and prints any line where the pattern is matched. To use grep, you need to provide it with a pattern and one or more filenames (or piped data).
Common command options
- -c
-
Count the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access