Introduction to regular expressions
In this section, you will learn about regular expressions and why we use them. Then we will discuss the usage of regular expressions in AWK.
What is a regular expression?
A regular expression, or regexpr, is a set of characters used to describe a pattern. A regular expression is generally used to match lines in a file that contain a particular pattern. Many Unix utilities operate on plain text files line by line, such as grep
, sed
, and awk
. Regular expressions search for a pattern on a single line in a file.
Note
A regular expression doesn't search for a pattern that begins on one line and ends on another. Other programming languages may support this, notably Perl.
Why use regular expressions?
Generally, all editors have the ability to perform search-and-replace operations. Some editors can only search for patterns, others can also replace them, and others can also print the line containing that pattern. A regular expression goes many steps beyond this simple...