Unit 3 Linux Regular Expression
Unit 3 Linux Regular Expression
Regular expression is also called regex or regexp. It is a very powerful tool in Linux.
Regular expression is a pattern for a matching string that follows some pattern.
Regex can be used in a variety of programs like grep, sed, vi, bash, rename and many
more.
Metacharacter Description
Regex Versions
There are three versions of regular expressions syntax:
Syntax:
Example:
1. grep t msg.txt
2. grep l msg.txt
3. grep v msg.txt
Look at the above snapshot, all the matching pattern lines are displayed and pattern
is highlighted.
Concatenating Characters
If a pattern is of concatenating characters then it has to be matched as it is, for the
line to be displayed.
Example:
1. grep tp msg.txt
2. grep in msg.txt
3. grep is msg.txt
Look at the above snapshot, lines matching exactly the specified patterns are
displayed.
are shown. Options -E and -P syntax are same but -G syntax uses (\).
Syntax:
Example:
the lines.
times occurence.
Syntax:
Example:
Syntax:
Example:
1. grep r$ dupli.txt
2. grep e$ dupli.txt
Look at the above snapshot, lines are displayed matching the end of a string.
Match The Start Of A String
To match the start or beginning of a file we use caret sign (^).
Syntax:
Example:
1. grep ^o dupli.txt
Look at the above snapshot, lines are displayed matching the start or beginning of
a string.
Separating Words
Syntax:
Example:
"grep '\bsome\b' file" only lines matching single word 'some' are displayed.
Syntax:
Example:
Look at the above snapshot, command "grep -w some file" displays the same
result as \b character.
Syntax:
Example:
1. rename 's/text/txt/' *
Look at the above snapshot, all the 'text' are converted into 'txt'.
Syntax:
Example:
Look at the above snapshot, all '.txt' are converted into '.TXT'.
In above two examples the strings used were present only at the end of the file name.
Example:
A Global Replacement
In the above example only first 'txt' was replaced in 'atxt.txt'. To replace both the
Syntax:
Example:
Look at the above snapshot, both the 'txt' are replaced with 'TXT'.
Syntax:
1. rename 's/.text/.txt/i' *
Look at the above snapshot, all '.text' are replaced with '.txt'.
Example:
Look at the above snapshot, string 'interactive' is changed to 'distractive' with sed
command. Inspite of forward slash (/), colon (:), underscore (_) and pipe (|) will also
work.
Interactive Editor
The sed command is meant to be stream editor while it can also be used as interactive
Look at the above snapshot, stream 'today' is converted into 'tomorrow' in the 'file'.
Look at the above snapshot, ampersand has searched the string 'four' and printed it
as 'fourfourty'.
Look at the above snapshot, date is printed in different formats. Here, 2014 is
White Space
The white space syntax is '\s' and tab space syntax is '\t'.
Look at the above snapshot, '\s' is used for a single space.
Optional Occurrence
You can specify something optional by specifying it with (?) question mark.
Look at the above snapshot, we have made third 'i' as optional. It mens that two 'i' are
Look at the above snapshot, we have specified exactly three times occurence of 'i'.
Occurence In Range
We can specify occurence in terms of range also. For example, if we'll specify range as
{m,n}, then 'm' denotes minimum times occurence and 'n' denotes maximum times
occurence.
Look at the above snapshot, we have specified minimum range as 3 and maximum
range as 4.