egrep - Linux
Overview
egrep
is a command-line utility in Unix and Linux systems, designed for text searching using patterns. The primary function of egrep
is to search a specified file or files and output the lines that contain matches to the specified pattern. egrep
uses extended regular expressions, which provide more flexibility and functionality compared to basic regular expressions used by grep
.
Syntax
egrep [options] pattern [file...]
pattern
: This denotes the regular expression thategrep
searches for. It must be quoted if it includes shell metacharacters.file...
: One or more files in which the search is performed. If no file is specified,egrep
searches the standard input.
Options/Flags
-v
or--invert-match
: Display all lines that do not match the pattern.-i
or--ignore-case
: Ignore case distinctions in both the pattern and the input files.-c
or--count
: Only print a count of matching lines per file.-l
or--files-with-matches
: Print only the names of files with selected lines.-n
or--line-number
: Prefix each line of output with the 1-based line number within its input file.-m
or--max-count
: Stops reading a file after a specified number of matching lines.-e
PATTERN: Specifies the pattern used during the search. Useful when searching for patterns that start with a hyphen.-f
FILE: Takes patterns from a file, one per line.-r
or--recursive
: Read all files under each directory, recursively.
Examples
-
Basic Search:
Search for the pattern “hello” in the filetext.txt
.egrep "hello" text.txt
-
Case Insensitive Search:
Search for “hello” intext.txt
, ignoring case.egrep -i "hello" text.txt
-
Invert Match:
Display lines that do not contain “hello”.egrep -v "hello" text.txt
-
Count Matches:
Count the number of lines that contain “hello”.egrep -c "hello" text.txt
-
Recursive Search:
Search recursively in the directorydocs
for the word “reference”.egrep -r "reference" docs/
Common Issues
- Pattern Not Found: If your pattern uses characters interpreted by the shell, it may not work as expected. Always quote patterns to avoid this issue.
- Binary File Matches: By default,
egrep
might search through binary files as well. Use the--binary-files
option to control this behavior. - Performance Issues: Complex patterns or large input files can cause slow search speeds. Optimizing your pattern or using incremental searches might help.
Integration
egrep
can be combined with other commands to form powerful command pipelines:
egrep -i "error" /var/log/syslog | less
This command chain searches for “error” in the syslog file, making the output easier to read using less
.
Related Commands
grep
: Basic pattern matching using basic regular expressions.fgrep
: Search for fixed strings instead of regular expressions.awk
: A programming language that can perform pattern scanning and processing.sed
: A stream editor that can perform more complex text transformations.
For more detail on using egrep
, consult the grep
man page (man grep
), as it covers basic grep
, egrep
, and fgrep
functionalities.