egrep command in Linux with examples
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.
egrep [ options ] 'PATTERN' files
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 seperated 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.