egrep command in Linux with examples
Last Updated :
02 Sep, 2024
The ‘egrep’ command in Linux is a powerful pattern-searching utility that belongs to the family of grep functions. It functions similarly to ‘grep -E’ by treating patterns as extended regular expressions, allowing more complex pattern matching without the need to escape special characters. This command is particularly useful for quickly searching through files and directories for specific text patterns.
What is the ‘egrep’ Command?
‘egrep’ stands for “extended grep” and is a part of the grep family of commands used for text processing and pattern matching in Linux. It is preferred over basic grep when working with extended regular expressions, as it inherently supports meta-characters without requiring them to be escaped, thus simplifying complex searches. The ‘egrep‘ command is also known for its speed and efficiency, making it a popular choice among system administrators and developers.
Syntax:
egrep [ options ] 'PATTERN' files
where,
- ‘[options]’: Flags that modify the command’s behavior.
- ‘PATTERN’: The regular expression pattern to search for.
- ‘[files]’: The files or directories where the search will be performed.
Example: 
How ‘egrep’ differs from ‘grep’
While both ‘egrep’ and ‘grep’ are used for pattern searching, the key difference lies in their handling of regular expressions. ‘egrep’ supports extended regular expressions by default, allowing the use of complex patterns without needing to escape special characters. This capability makes ‘egrep‘ faster and more efficient in scenarios where advanced pattern matching is required.
Common Options with ‘egrep’ Command
Here are some of the most commonly used options for ‘egrep’, along with examples to illustrate their usage:
1. ‘-c’ (Count Matches)
Used to counts and prints the number of lines that matched the pattern and not the lines. 
2. ‘-v’ (Invert Match)
It prints the lines that does not match with the pattern. 
3. ‘-i’ (Ignore Case)
Ignore the case of the pattern while matching. 
4. ‘-l’ (List Matching Files)
Prints only the names of the files that matched. It does not mention the matching line numbers or any other information. 
5. ‘-L’ (List Non-Matching Files)
Prints only the names of the files that did not have the pattern. Opposite of -l flag. 
6. ‘-e’ (Pattern List)
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. 
7. ‘-w’ (Match Whole Words)
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. 
8. ‘-x’ (Match Entire Lines)
Prints only those lines that matches an entire line of the file. 
9. ‘-m NUMBER’ (Limit Matches)
Continue to search for matches till the count reaches NUMBER mentioned as argument. 
10. ‘-o’ (Print Only Matching Parts)
Prints only the matched parts of the line and not the entire line for each match. 
11. ‘-n’ (Line Numbers)
Prints each matched line along with the respective line numbers. For multiple files, prints the file names along with line numbers. 
12. ‘-r’ (Recursive Search)
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. 
Conclusion
The ‘egrep’ command is a powerful tool in the Linux command, offering advanced pattern matching capabilities that simplify searching through text files. With its ability to handle extended regular expressions efficiently and a wide range of options, ‘egrep’ can significantly enhance productivity when working with large datasets or complex text searches.
Similar Reads
dmesg command in Linux for driver messages
dmesg command also called âdriver messageâ or âdisplay messageâ is used to examine the kernel ring buffer and print the message buffer of the kernel. The output of this command contains the messages produced by the device drivers. Usage of dmesg:When the computer boots up, there are lot of messages(
6 min read
dmidecode command in Linux with Examples
The 'dmidecode' command, short for Desktop Management Interface table decoder, is a powerful utility in Linux that retrieves detailed information about your system's hardware components. This tool reads the DMI table and presents the data in a human-readable format, making it invaluable for system a
4 min read
domainname Command in Linux With Examples
domainname command in Linux is used to return the Network Information System (NIS) domain name of the host. You can use hostname -d command as well to get the host domainname. If the domain name is not set up in your host then the response will be "none". In networking terminology, the domain name i
3 min read
dos2unix and unix2dos commands
Sometimes, you will need to move files between windows and unix systems. Window files use the same format as Dos, where the end of line is signified by two characters, Carriage Return or CR or \r followed by Line Feed or LF or \n. Unix files, on the other hand, use only Line Feed (\n). unix2dos is a
4 min read
dosfsck command in Linux with Examples
dosfsck stands for DOS File System Consistency Check and is part of the dosfstools package in Unix-like operating systems. This command diagnoses and repairs MS-DOS filesystems (FAT12, FAT16, FAT32). Before running dosfsck, it is crucial to unmount the filesystem to avoid data corruption. Syntax:dos
4 min read
dstat Command in Linux With Examples
dstat is a tool that is used to retrieve information or statistics form components of the system such as network connections, IO devices, or CPU, etc. It is generally used by system administrators to retrieve a handful of information about the above-mentioned components of the system. It itself perf
3 min read
du Command in LINUX
The du command in Linux is a powerful utility that allows users to analyze and report on disk usage within directories and files. Whether youâre trying to identify space-hogging directories, manage disk space efficiently, or simply gain insights into storage consumption, the du command provides valu
5 min read
dump command in Linux with examples
The dump command in Linux is used for backing up the filesystem to a storage device. Unlike commands that back up individual files, dump operates on entire filesystems, transferring data to tape, disk, or other storage media for safekeeping. One of its key advantages is the ability to perform increm
6 min read
dumpe2fs command in Linux with examples
dumpe2fs command is used to print the super block and blocks group information for the filesystem present on device. Can be used with ext2/ext3/ext4 filesystem for information. The printed information may be old or inconsistent when it is used with a mounted filesystem. Don't forget to unmount your
2 min read
dumpkeys command in Linux with examples
'dumpkeys' command in Linux is used for the dump keyboard translation tables. It writes to the standard output and the current contents of the keyboard driver's translation tables in the format specified by Keymaps. Here, weâll break down the syntax, options, and practical examples of using the dump
3 min read