0% found this document useful (0 votes)
24 views

Linux Regular Expression Tutorial - Grep Regex Example

Uploaded by

hbaltaji8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Linux Regular Expression Tutorial - Grep Regex Example

Uploaded by

hbaltaji8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

10/3/23, 8:42 AM Linux Regular Expression Tutorial: Grep Regex Example

Linux Regular Expression Tutorial: Grep Regex


Example
By : Mary Brent Updated August 14, 2023

What are Linux Regular Expressions?


Linux Regular Expressions are special characters which help search data and
matching complex patterns. Regular expressions are shortened as ‘regexp’ or
‘regex’. They are used in many Linux programs like grep, bash, rename, sed, etc.

Table of Content:

Types of Regular expressions


For ease of understanding let us learn the different types of Regex one by one.

Click here if the video is not accessible

https://www.guru99.com/linux-regular-expressions.html 1/8
10/3/23, 8:42 AM Linux Regular Expression Tutorial: Grep Regex Example

Basic Regular expressions


Some of the commonly used commands with Regular expressions are tr, sed, vi and
grep. Listed below are some of the basic Regex.

Symbol Descriptions

. replaces any character

^ matches start of string

$ matches end of string

* matches up zero or more times the preceding character

\ Represent special characters

() Groups regular expressions

? Matches up exactly one character

Let’s see an example.

Execute cat sample to see contents of an existing file

Search for content containing letter ‘a’.

https://www.guru99.com/linux-regular-expressions.html 2/8
10/3/23, 8:42 AM Linux Regular Expression Tutorial: Grep Regex Example

‘^‘ matches the start of a string. Let’s search for content that STARTS with a

Only lines that start with character are filtered. Lines which do not contain the
character ‘a’ at the start are ignored.

Let’s look into another example –

Select only those lines that end with t using $

Interval Regular expressions


These expressions tell us about the number of occurrences of a character in a
string. They are

https://www.guru99.com/linux-regular-expressions.html 3/8
10/3/23, 8:42 AM Linux Regular Expression Tutorial: Grep Regex Example

Expression Description

{n} Matches the preceding character appearing ‘n’ times exactly

Matches the preceding character appearing ‘n’ times but not more
{n,m}
than m

Matches the preceding character only when it appears ‘n’ times or


{n, }
more

Example:

Filter out all lines that contain character ‘p’

We want to check that the character ‘p’ appears exactly 2 times in a string one after
the other. For this the syntax would be:

cat sample | grep -E p\{2}

Note: You need to add -E with these regular expressions.

Extended regular expressions


These regular expressions contain combinations of more than one expression.
Some of them are:

https://www.guru99.com/linux-regular-expressions.html 4/8
10/3/23, 8:42 AM Linux Regular Expression Tutorial: Grep Regex Example

Expression Description

\+ Matches one or more occurrence of the previous character

\? Matches zero or one occurrence of the previous character

Example:

Searching for all characters ‘t’

Suppose we want to filter out lines where character ‘a’ precedes character ‘t’

We can use command like

cat sample|grep "a\+t"

Brace expansion
The syntax for brace expansion is either a sequence or a comma separated list of
items inside curly braces “{}”. The starting and ending items in a sequence are
separated by two periods “..”.

Some examples:

https://www.guru99.com/linux-regular-expressions.html 5/8
10/3/23, 8:42 AM Linux Regular Expression Tutorial: Grep Regex Example

In the above examples, the echo command creates strings using the brace
expansion.

Summary:
Regular expressions are a set of characters used to check patterns in strings
They are also called ‘regexp’ and ‘regex’
It is important to learn regular expressions for writing scripts
Some basic regular expressions are:

Symbol Descriptions

. replaces any character

^ matches start of string

$ matches end of string

Some extended regular expressions are:

Expression Description

\+ Matches one or more occurrence of the previous character

\? Matches zero or one occurrence of the previous character

Some interval regular expressions are:

https://www.guru99.com/linux-regular-expressions.html 6/8
10/3/23, 8:42 AM Linux Regular Expression Tutorial: Grep Regex Example

Expression Description

{n} Matches the preceding character appearing ‘n’ times exactly

Matches the preceding character appearing ‘n’ times but not more
{n,m}
than m

Matches the preceding character only when it appears ‘n’ times or


{n, }
more

The brace expansion is used to generate strings. It helps in creating multiple


strings out of one.

You Might Like:

Unix vs Linux – What is Difference Between Them


15 Best FREE SFTP Server Software for Windows (2023)
6 Best Free TFTP Server for Windows (Oct 2023 List)
10 BEST Linux Distros (2023)
How to Unzip Files in Linux and Ubuntu

Prev Report a Bug Next

AD

Wireless Keyboard and Mouse Set, 2.4 GHz Wireless USB Keyboard and Mouse
Combo with USB recevier, Slim Ergonomic Keyboard for Windows, Computer, P…
443
28% off Deal
£ 16.61 £22.99

https://www.guru99.com/linux-regular-expressions.html 7/8
10/3/23, 8:42 AM Linux Regular Expression Tutorial: Grep Regex Example

About
About Us
Advertise with Us
Write For Us
Contact Us

Career Suggestion
SAP Career Suggestion Tool
Software Testing as a Career

Interesting
eBook
Blog
Quiz
SAP eBook

Execute online
Execute Java Online
Execute Javascript
Execute HTML
Execute Python

English © Copyright - Guru99 2023 Privacy


Policy | Affiliate Disclaimer | ToS

https://www.guru99.com/linux-regular-expressions.html 8/8

You might also like