Open In App

egrep command in Linux with examples

Last Updated : 13 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

egrep is a pattern searching command which belongs to the family of grep functions. It works the same way as grep -E does. It treats the pattern as an extended regular expression and prints out the lines that match the pattern. If there are several files with the matching pattern, it also displays the file names for each line. Syntax:

egrep [ options ] 'PATTERN' files 

Example: Note: The egrep command used mainly due to the fact that it is faster than the grep command. The egrep command treats the meta-characters as they are and do not require to be escaped as is the case with grep. This allows reducing the overhead of replacing these characters while pattern matching making egrep faster than grep or fgrep. Options: Most of the options for this command are same as grep.

  • -c: Used to counts and prints the number of lines that matched the pattern and not the lines.
  • -v: It prints the lines that does not match with the pattern.
  • -i: Ignore the case of the pattern while matching.
  • -l: Prints only the names of the files that matched. It does not mention the matching line numbers or any other information.
  • -L: Prints only the names of the files that did not have the pattern. Opposite of -l flag.
  • -e: Allows to use a ‘-‘ sign in the beginning of the pattern. If not mentioned the shell tries to execute the pattern as an option and returns an error.
  • -w: Prints only those lines that contain the whole words. Word-constituent characters are letters, digits and underscore. The matching substring must be separated by non-word constituent characters.
  • -x: Prints only those lines that matches an entire line of the file.
  • -m NUMBER: Continue to search for matches till the count reaches NUMBER mentioned as argument.
  • -o: Prints only the matched parts of the line and not the entire line for each match.
  • -n: Prints each matched line along with the respective line numbers. For multiple files, prints the file names along with line numbers.
  • -r: Recursively search for the pattern in all the files of the directory. The last argument is the directory to check. ‘.’ (dot) represents the current directory.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads