Linux egrep Command Tutorial with Examples – POFTUT

Linux egrep Command Tutorial with Examples


Linux provides grep for text filtering. But in some situations we may need more extended tool to match given pattern in given text files. This tool is called Extended Global Regular Expression Print or simply egrep . egrep provides extended features over regular grep tool.

Syntax

Syntax of  egrep command is like below.

egrep FLAG REGEX FILE
  • `FLAG` is used to change some behaivour of `egrep`command.
  • `REGEX` is the pattern we want to search in regex format.
  • `FILE` is the file name we will search in

Search Normally

We can use egrep to search normal text without providing regular expression. We just need provide the term we want to search. In this example we will search ismail in the file named /etc/passwd .

$ egrep ismail /etc/passwd
Search Normally
Search Normally

Match Lines Contains Numeric Characters

We can specify numeric characters by using [0-9] which means one of numeric value from 0 to 9.

$ egrep '[0-9]'/etc/passwd

Match Lines Contains Alphabet Characters

We can also specify alphabet characters with [a-z] for lowercase characters and [A-Z] for uppercase characters. In this example we will match upper case characters.

$ egrep '[A-Z]'/etc/passwd
Match Lines Contains Alphabet Characters
Match Lines Contains Alphabet Characters

Match All Lines Starting with Alphabet

We can match the start of the lines with ^ sign. In this example we will look all lines where starts with alpha character.

$ egrep '^[a-Z]' myinput.py
Match All Lines Starting with Alphabet
Match All Lines Starting with Alphabet

Match All Lines Ending with Numeric

We can specify the end of line with $ sign. Following command will list all lines those ends with a numeric character.

$ egrep '[0-9]$' myinput.py

Match Caseinsenstive

egrep is case senstive by default. Case sensitive means upper and lower case characters will be different like A is different than a. We can made our match caseinsensitive if we want with -i option as flag. Following example will match all of following words

  • IsmaiL
  • ismAIL
  • ISMAIL
$ egrep -i 'ismail' myinput.py

LEARN MORE  Grep and Filter IP Address In Linux

1 thought on “Linux egrep Command Tutorial with Examples”

Leave a Comment