Linux Foundation Certified System Administrator (LFCS)

Essential Commands

Extended Regular Expressions

Extended regular expressions (ERE) simplify pattern matching by reducing the need for escaping special characters. When using ERE with grep (via the -E option or its alias, egrep), most special characters are interpreted as regex operators by default. You only need to escape them when you want them treated as literal characters.

Tip

Use ERE with grep by using the uppercase -E flag or the egrep command to simplify your regular expressions and avoid common pitfalls with escaping characters.

Basic Usage with grep

Consider a command that searches for one or more occurrences of the digit zero in files under the /etc/ directory:

$ grep -Er '0+' /etc/

This command uses the + operator to match one or more zeros. Equivalently, you could use:

$ egrep -r '0+' /etc/

In both cases, the command highlights lines in various /etc/ files where the pattern is found.

Matching Specific Repetitions

To find strings containing at least three consecutive zeros, you can use the curly bracket syntax:

$ egrep -r '0{3,}' /etc/

Here, {3,} specifies a minimum repetition of three, with no upper limit.

If you want to search for a string beginning with 1 followed by up to three zeros, the pattern is:

$ egrep -r '10{,3}' /etc/

This regex also matches the case where no zero follows the digit 1. To match exactly three zeros, omit the comma and second number:

$ egrep -r '0{3}' /etc/

Optional Characters with the Question Mark

The question mark operator (?) makes the preceding element optional (i.e., it can appear once or not at all). For example, to find lines containing either "disable" or "disabled," you might use:

$ egrep -r 'disable?d?' /etc/

Be cautious—this expression may also match portions of longer words (like "disables"). To match whole words exactly, consider using the -w option with grep or an alternation operator:

$ egrep -r 'enabled|disabled' /etc/

For broader matching that handles case variations, add the -i option for a case-insensitive search.

Ranges and Sets

Ranges allow you to specify a set of characters between two endpoints. For example:

  • [a-z] matches any lowercase letter.
  • [0-9] matches any digit.

Sets allow matching one character from a list. To search for either "cat" or "cut":

$ egrep -r 'c[au]t' /etc/

This pattern checks for the letters a or u in the middle of "c?t," effectively matching both "cat" and "cut."

Combining Regex Patterns: Matching Device Files

When matching configuration entries for device files (e.g., /dev/sda1 or /dev/twa0), the naive use of .* might be too broad:

$ egrep -r '/dev/.*' /etc/

Instead, you can be more specific by matching a forward slash followed by any number of lowercase letters:

$ egrep -r '/dev/[a-z]*' /etc/

To include trailing digits, adjust the pattern:

$ egrep -r '/dev/[a-z]*[0-9]' /etc/

This matches only device names ending in a digit. To accommodate both cases (with or without a trailing digit):

$ egrep -r '/dev/[a-z]*[0-9]?' /etc/

For multi-segment device names (like /dev/tty0p0), group the pattern for letters and an optional digit, then allow repetition:

$ egrep -r '/dev/([a-z]*[0-9]?)+'

If uppercase letters are also possible in device names, extend the character class:

$ egrep -r '/dev/(([a-zA-Z])*[0-9]?)+' /etc/

This pattern effectively matches various formats, including /dev/ttyS0.

Using the Negation Operator

Inside square brackets, the caret (^) negates a set. For example, to search for the string "https" that is not immediately followed by a colon:

$ egrep -r 'https[^:]' /etc/

Similarly, you can refine your pattern to match "http" not followed by certain characters by excluding them in the character set.

For example, to find lines where a forward slash is immediately followed by a character that is not a lowercase letter:

$ egrep -r '/[^a-z]' /etc/

This command will return lines where the character following / does not fall within the lowercase alphabet.

Practical Considerations and Further Resources

Regular expressions provide a powerful, precise method for text searching and manipulation. By mastering regex operators, ranges, sets, and grouping, you can craft expressions tailored to your needs.

Further Study

Explore online tools such as regexr.com to experiment with and validate your regular expressions. Additionally, refer to the grep documentation for more detailed information.

Happy grepping!

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Analyze Text Using Basic Regular Expressions