Linux egrep Command Tutorial

The egrep command is an extended version of the grep command. The egrep name comes from the Extended Global Regular Expression Print . The egrep command simply searches single or multiple specified files for a specific pattern which can be also a Regular Expression. The egrep command is the grep -E implementation where the -E is used for extended regex operations.

egrep Command Syntax

The egrep command syntax is like below.

egrep OPTION REGEX FILE
  • OPTION is used to set different options for the egrep command.
  • REGEX is the string or regular expression pattern to search.
  • FILE is a single file, multiple files, or a path to search specified REGEX.

Match String

The egrep command can be used to math specified string for the specified files. In the following example, we search for the “root” in the file named “/var/log/syslog”.

$ egrep root /var/log/syslog

Match Regex

The egrep command is specifically created for the regular expressions. We can specify the search term as regular expression. In the following example, we search for 2 digits using the [0-9][0-9] regex.

$ egrep '[0-9][0-9]' /var/log/syslog

Print File Names

By default, the egrep command does not print the file names of the matches. The -H option is used to print matches with file names.

$ egrep -R -H 'root' /var/log/

Ignore Case

The egrep is case sensitive command which means provided term is searched in a case-sensitive way. The uppercase and lowercase letters are interpreted as different. The -i option can be used to disable case sensitivity and make the search ignore the case.

See also  Find Files In Linux From Command Line

Print Only Match Count

Another useful feature of the egrep command is printing the matched line count. The -C option is used to print matched line count.

$ egrep -R -C 'root' /var/log/

Leave a Comment