We can use the grep tool provided by linux to find matching rows based on either the basic or extended regular expressions.

Options for grep and egrep command

Here are some options for grep and egrep commands.

Matcher Selection

Option Meaning
-E or ‑‑extended-grep Treat the PATTERNS as extended regular expressions.
-F or ‑‑fixed-strings Treat the PATTERNS as fixed strings instead of regular expressions.
-G or ‑‑basic-regexp Treat the PATTERNS as basic regular expressions.
-P or ‑‑perl-regexp Treat the PATTERNS as Perl-compatible regular expressions (PCREs).

Matching Control

Option Meaning
-e PATTERNS or ‑‑regexp=PATTERNS Use PATTERNS as the patterns.
-f FILE or ‑‑file=FILE Obtain patterns from FILE, this can be used multiple times or combined with the -e (‑‑regexp) option.
-i or ‑‑ignore-case Ignore case sensitivity, this matches upper and lower cases.
-v or ‑‑invert-match Output the non-matching lines.
-w or ‑‑word-regexp Output only lines that match the entire word and have no effect when used with the -x option.
-x or ‑‑line-regexp Output only lines that match the entire line.

General Output Control

Option Meaning
-c or ‑‑count Outputs the number of rows that match or does not match rather than the contents of the output line.
‑‑color[=WHEN] or ‑‑colour[=WHEN] Identify the matching content with color. WHEN could be never, always, or auto.
-m NUM or ‑‑max-count=NUM Output the first NUM matches and ignore the followings.
-o or ‑‑only-matching Output only what matches, and output them on different lines.
-q or ‑‑quiet or ‑‑silent If there is a content match, the exit code is 0, and if there is no content to match, the exit code is 1.
-s or ‑‑no-message Suppress error messages such as File not present or File unreadable.

Output Line Prefix Control

Option Meaning
-n or ‑‑line-number Prefix the line number for each line that matches or does not match.

Context Line Control

Option Meaning
-A NUM or ‑‑after-context=NUM This will use the ‑‑ separator to separate adjacent matching groups.
-B NUM or ‑‑before-context=NUM This will use the ‑‑ separator to separate adjacent matching groups.
-C NUM or ‑‑context=NUM This will use the ‑‑ separator to separate adjacent matching groups.

Regular Expressions

Here are some regular expressions for grep command.

Character Classes and Bracket Expressions

Option Meaning
[0123456789] or [0-9] or [:digit:] Match any single digit.
[:alnum:] or [0-9A-Za-z] Match any single alphabet and digit.
[:alpha:] or [A-Za-z] Match any single alphabet.
[:lower:] Match any single lower case alphabet.
[:upper:] Match any single upper case alphabet.
[:punct:] Match any punctuation.
[:space:] Match any empty space.

Anchoring

Option Meaning
^ Match the empty string at the beginning of a line.
$ Match the empty string at the end of a line.

The Backslash Character and Special Expressions

Option Meaning
\< Match the empty string at the beginning of a word.
\> Match the empty string at the end of a word.
\b Match the empty string at the end of a word.
\B Match the empty string if it is not at the end of a word.

Repetition

Option Meaning
. Match any single character.
? Match preceding item at most once and its optional.
* Match preceding item zero or more times.
+ Match preceding item one or more items.
{n} Match preceding item exactly n times.
{n,} Match preceding item exactly n or more times
{,m} Match preceding item at most m times.
{n,m} Match preceding item at least n times, but not more than m times.

Alternation

Option Meaning
| Matches the values on both sides of the pipe from left to right and returns with one match.

Back References and Subexpressions

Option Meaning
\n n is a digit refers to the nth group of matching groups.

Basic vs Extended Regular Expressions

Used in Basic Used in Extended
\? ?
\+ +
\{ {
\| |
\( (
\) )

Basic Regular Expression Examples

Before we get started, let’s prepare some test content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
printf '%s\n' \
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam nisi tellus,' \
'egestas sed dolor nec, eleifend venenatis mi. Mauris ac ligula' \
'scelerisque libero interdum varius. Aenean nec arcu nec turpis' \
'commodo mollis. Morbi suscipit felis libero, eu sollicitudin justo' \
'condimentum id. Praesent eu metus rutrum, varius leo malesuada,' \
'varius sapien. Vestibulum blandit, dolor bibendum bibendum ultricies,' \
'ipsum velit pretium arcu, eu maximus augue massa vitae justo. Etiam' \
'elit sapien, placerat quis libero a, tincidunt ornare erat. Praesent eu nibh' \
'ac sem mattis tincidunt. Aliquam erat volutpat. Nulla egestas dui ac urna' \
'convallis, vel egestas lectus ultrices. Morbi sit amet quam eget tortor' \
'laoreet laoreet id non libero.' > grep_file

Match Lines Start With Specified Content

1
cat vim_file | grep '^e'
img

Match Lines End With Specified Content

1
cat vim_file | grep 'a$'
img

Match Lines Contain Either of the Specified Words

1
cat vim_file | grep 'do\|ma'    # The backslash(\) is needed in basic regular expression
img

Extended Regular Expression Examples

The command egrep is equivalent to grep -E.

Before we get started, let’s prepare some test content:

1
2
3
4
5
6
printf '%s\n' \
'These are IPs for related computers' \
'PC1 IP is: 192.168.0.1' \
'PC2 IP is: 192.168.1.2' \
'PC3 IP is: 192.168.10.255' \
'PC4 IP is: unknown' > egrep_file

Match IPv4 Addresses

Match Numbers Between 0 and 255

This expression matches between 0 and 255 and any number between them:

1
cat egrep_file | egrep '\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
img

Match (0-255).(0-255).(0-255). Format

This expression matches three times between 0 and 255 and any number between them, each followed by a dot:

1
cat egrep_file | egrep '(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}'
img

Match (0-255).(0-255).(0-255).(0-255) Format IPv4 Addresses

This expression matches four times between 0 and 255 and any number between them, each followed by a dot, except the last one:

1
cat egrep_file | egrep '(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
img

References 8.5 Regular Expressions, GREP(1)

Buy me a coffeeBuy me a coffee