The egrep
command is an extended version of grep
that allows for more complex pattern matching using regular expressions. It is commonly used to search through text files for lines that match a specified pattern, making it a powerful tool for text processing.
The basic syntax of the egrep
command is as follows:
egrep [options] [arguments]
-i
: Ignore case distinctions in patterns and input data.-v
: Invert the match, showing lines that do not match the pattern.-c
: Count the number of lines that match the pattern.-n
: Show line numbers along with matching lines.-r
or -R
: Recursively search through directories.Here are some practical examples of using egrep
:
egrep "error" logfile.txt
This command searches for the word “error” in logfile.txt
.
egrep -i "warning" logfile.txt
This command searches for “warning” in logfile.txt
, ignoring case.
egrep -c "success" logfile.txt
This command counts how many lines contain the word “success”.
egrep -n "failed" logfile.txt
This command displays lines containing “failed” along with their line numbers.
egrep -r "TODO" /path/to/directory/
This command searches for “TODO” in all files within the specified directory and its subdirectories.
egrep
with other commands using pipes for more complex data processing.