Linux grep Regular Expressions

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Linux grep Regular Expressions

The grep tool has the following options to use regular expressions:

  • -E : String is read as ERE (Extended Regular Expressions)
  • -G : String is read as BRE (Basic Regular Expressions)
  • -P : String is read as PRCE (Perl Regular Expressions)
  • -F : String is read literally.

Print Lines Matching A Pattern

The grep command will search for line that matches the specified pattern.

Syntax:

grep <pattern> <fileName>

Example:

  1. grep t msg.txt  
  2. grep l msg.txt  
  3. grep v msg.txt 

Linux Grep Regular Expression1

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

Linux Grep Regular Expression2

Look at the above snapshot, lines matching exactly the specified patterns are displayed.

One Or The Other

Here pipe (|) symbol is used as OR to signify one or the other. All the three versions are shown. Options -E and -P syntax are same but -G syntax uses ().

Syntax:

  1. grep <option> <'pattern|pattern> <fileName>  

Example:

  1. grep -E 'j|g' msg.txt  
  2. grep -P 'j|g' msg.txt  
  3. grep -G 'j|g' msg.txt

Linux Grep Regular Expression3

Look at the above snapshot, either pattern 'j' or 'g' should be matched to display the lines.


One Or More / Zero Or More

The * signifies zero or more times occurence of a pattern and + signifies one or more times occurence.

Syntax:

grep <option> <'pattern*'> <fileName> 

Example:

  1. grep -E '1*' list  
  2. grep -E '1+' list  

Linux Grep Regular Expression4

Look at the above snapshot, * character displays zero or more times occurence of pattern '1'. But + character displays one or more times occurence.


Match The End Of A String

To match the end of a string we use $ sign.

Syntax:

  1. grep <pattern><fileName>  

Example:

  1. grep r$ dupli.txt  
  2. grep e$ dupli.txt 

Linux Grep Regular Expression5

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:

  1. grep ^<pattern> <fileName>  

Example:

grep ^o dupli.txt 

Linux Grep Regular Expression6

Look at the above snapshot, lines are displayed matching the start or beginning of a string.


Separating Words

Syntax:

grep '<pattern>' <fileName> 

Example:

grep 'some' file

Linux Grep Regular Expression7

Look at the above snapshot, by giving command "grep some file all the lines matching to the word 'some' are displayed. But by giving command "grep 'some' file" only lines matching single word 'some' are displayed.

Note: This can also be done with the help of -w option.

Syntax:

grep -w <pattern> <fileName>

Example:

grep -w some file 

Linux Grep Regular Expression8

Look at the above snapshot, command "grep -w some file" displays the same result as  character.