Skip to main content
In this lesson, we combine find with powerful regular expressions in grep to perform advanced text analysis on Linux. Previously, we used simple patterns like searching for “CentOS.” Now, we’ll tackle more complex tasks—such as extracting every IP address (e.g., 203.102.3.5) from multiple scattered files—by writing concise regex patterns that match exactly what you need. Regular expressions let you define constraints, much like restricting a variable ( x ) in mathematics:
The image shows a number line with conditions for an integer ( x ) where ( x > 3 ) and ( x < 8 ). The numbers 3 and 8 are marked, with question marks in between.
By combining operators, you create a single pattern that matches only what you allow.

Common Regular Expression Operators

Below is a quick reference to essential regex operators—mastering these accelerates file searches and log parsing:
The image displays a set of regex operators, including symbols like ^, $, ., *, +, {}, ?, |, [], (), and [^].

Anchors: Matching Line Boundaries

^ (Start of Line)

Restrict your search to the beginning of each line:
You can apply the same technique to system files. For instance, find lines in /etc/login.defs that start with PASS:
The image shows a dark-themed command-line interface with the text "The line begins with" at the top. The prompt is ready for input, and "KodeKloud" is visible in the corner.

$ (End of Line)

Match patterns only at the end of lines:

Wildcard: The Dot .

The dot . matches exactly one character. To search for any three-letter word starting with ‘c’ and ending with ‘t’:
To match whole words rather than substrings, add -w:

Escaping Metacharacters

If you need to match a literal metacharacter (e.g., a dot), escape it with a backslash:

Quantifiers: * and +

* (Zero or More)

The asterisk matches zero or more occurrences of the previous element:
Combine . and * to match any sequence between delimiters:
The image shows a dark-themed terminal interface with a command line prompt and a description of the asterisk (*) symbol, indicating it matches the previous element zero or more times.

+ (One or More)

In basic grep, + is literal unless escaped. To require at least one occurrence:
The image shows a dark-themed command-line interface with a prompt and a description of the "+" symbol, indicating it matches the previous element one or more times.
Basic grep treats +, ?, {}, |, and () as literals. To use them without escaping, switch to extended regex mode:

Next Steps

With these operators in your toolkit, you can build advanced patterns to extract IPs, parse log entries, and automate complex text-processing tasks across your Linux systems.

References

Watch Video