×
grep regex examples and tutorial

Grep Regex is one of the most popular command-line utilities to find and search strings in a text file.

Using the grep command with regular expressions makes it even more powerful.

Regular expressions come in the picture when you want to search for a text containing a particular pattern.

It simplifies your search operation by searching the patterns on each line of the file.

In this tutorial, we’ll show you how to perform advance string searches using Grep with regular expression with hands-on examples.

Regular Expression Overview

Regular expressions, also known as regex, are special characters that help you search for data and match complex patterns.

Some of the most commonly used regular expressions and their usage are shown below.

Commonly Used Regex Patterns:

  • . : Matches a single character.
  • ^ : Matches the beginning of the line.
  • $ : Matches the end of the line.
  • * : Matches the preceding character zero or more times.
  • ? : Matches the preceding character zero or one time.
  • [] : Matches any one of set characters.
  • () : Groups regular expressions.
  • \ : Matches special characters.
  • + : : Matches one or more occurrence of the previous character.
  • {n} : Matches the preceding character exactly n times.
  • {n,m} : Matches the preceding character at least n times and not more than m times.
  • {n,} : Matches the preceding character n times or more.

Regular Expression (.)

The regular expression “.” (dot) matches a single character.

To understand this better, let’s create a test.txt with the following contents:

cat test.txt

Output:

hitesh.sh
hitesh1.sh
hitesh2.sh
hitesh3.sh
hitesh.ksh
Hi rital
Hi mital how are you
sital is a good girl

Lets search for a word which has any one character followed by “ital“:

grep ".ital" test.txt

You should see the following output:

Hi rital
Hi mital how are you
sital is a good girl

Next, search for a line that contains a word with only 5 characters as shown below:

grep -w "....." test.txt

You should see the following output:

Hi rital
Hi mital how are you
sital is a good girl

Example:

Regex period example

Next, search for a word starting with “hitesh” and followed by another character:

grep "hitesh." test.txt

You should see the following output:

hitesh.sh
hitesh1.sh
hitesh2.sh
hitesh3.sh
hitesh.ksh

Next, search for a word starting with “hitesh” but ending with “sh” as shown below:

grep "hitesh..sh" test.txt

You should see the following output:

hitesh1.sh
hitesh2.sh
hitesh3.sh
hitesh.ksh

Example:

Regex period example 2

Regular Expression (^)

The regular expression (^) matches the expression at the start of a line.

Let’s create a sample test.txt file with the following contents:

cat test.txt

Output:

balaram

balaram is a good boy

My name is balaram
balarampur is a village

Example:

Regex ^ example

Now, lets display all the lines that start with the string balaram:

grep "^balaram" test.txt

You should see the following output:

balaram
balaram is a good boy
balarampur is a village

Next, display all the lines which start with the word balaram only:

grep -w "^balaram" test.txt

You should see the following output:

balaram
balaram is a good boy

Next, find the number of blank lines in the file test.txt:

grep "^$" test.txt

You should see the following output:

3

Example:

Regex ^ example

Regular Expression ($)

The regular expression ($) matches the expression at the end of a line.

Let’s create a sample test.txt file with the following content:

cat test.txt

Output:

My name is vyom
vyom is a good boy
vyom

For example, find all the lines which end with the word vyom:

grep "vyom$" test.txt

You should see the following output:

My name is vyom
vyom

Next, find all the lines which start and end with the word vyom:

grep "^vyom$" test.txt

You should see the following output:

vyom

Example:

Regex $ example

Regular Expression (*), (\?) and (\+)

The regular expression (*) matches zero or more occurrence of the previous character.

Let’s, create a sample test.txt file with the following contents:

cat test.txt

Output:

vyom
My name is vyom
vyom is a good boy
vyomis a student
vyom is a clever boy

Now, search for a pattern “vyom” followed by any number of spaces/no space:

grep "vyom*" test.txt

You should see the following output:

vyom
My name is vyom
vyom is a good boy
vyomis a student
vyom is a clever boy

The regular expression (\?) matches zero or one occurrence of the previous character.

For example, search for a pattern “vyom is” followed by single space or no space.

grep "vyom \?is" test.txt

You should see the following output:

vyom is a good boy
vyomis a student

The regular expression (\+) matches one or more occurrence of the previous character.

For example, search for a pattern “vyom is” followed by single or more space:

grep "vyom \+is" test.txt

You should see the following output:

vyom is a good boy
vyom is a clever boy

Example:

Regex $ example

Regular Expression {n}, {n,} and {n,m}

The regular expression {n} matches the preceding character appearing ‘n’ times exactly.

Let’s create a sample test.txt file with the following contents:

cat test.txt

Output:

apple
appple
appppple

Now, search for all the lines which match a character “p” two times:

grep -E "ap{2}l" test.txt

You should see the following output:

apple

Next, search for all the lines which match a character “p” two or more times:

grep -E "ap{2,}l" test.txt

You should see the following output:

apple
appple
appppple

Next, search for all the lines which match a character “p” two or three times:

grep -E "ap{2,3}l" test.txt

You should see the following output:

apple
appple

Example:

Regex n example

Regular Expression (\)

The regular expression (\) used to search for special characters.

Let’s create a sample test.txt file with the following contents:

cat test.txt

Output:

1.1.1.1
1a1a1a1
1b1c1d1

Now, search for all the lines which matches the pattern “1.1.1.1“:

grep "1.1.1.1" test.txt

This command does not show the proper result as “.” matches any single character:

1.1.1.1
1a1a1a1
1b1c1d1

You can use the regular expression “\” to resolve this issue:

grep "1\.1\.1\.1" test.txt

Output:

1.1.1.1

Example:

Regex n example

Regular Expression []

The regular expression [] can be used to match any one character found within the bracket group.

For example, create a sample test.txt file with the following contents:

cat test.txt

Output:

rajesh
gajesh
majesh
testx
testy
testz
hitesh
HitEsh

Now, search for all the lines which match any one character found within the “ajesh” group.

grep "[rgm]ajesh" test.txt

You should see the following output:

rajesh
gajesh
majesh

Next, search for all the lines which match any range character found within the “test” group.

grep "test[x-z]" test.txt

You should see the following output:


testx
testy
testz

Next, search for all the lines which match the word hitesh and HitEsh:

grep "[Hh]it[Ee]sh" test.txt

You should see the following output:

hitesh
HitEsh

Example:

Regex [] example

Conclusion

As you can see from all of the examples above, the Regular Expression is a very useful tool to search for any complex matching patterns.

Regular expressions can be used with many popular programs and learning them can help you perform many functions within minutes instead of hours of manual filtering and sorting!

We hope this GREP Regex Tutorial was useful and if you have any comments or questions, please post them below!