Cart 0
No products in the cart.

How to use egrep command in Linux ?

How to use egrep command in Linux ?

The egrep command is a type of grep command that is used to search for patterns in files. It is an extended version of the grep command and supports the use of extended regular expressions. The egrep command stands for "Extended Global Regular Expression Print". It is used to search for patterns in multiple files and can also be used to search for patterns in the standard input. 

Here at Fixwebnode, we shall look into different ways of using egrep.

 

Table of contents [Show]

What is egrep Command syntax ?

The syntax of the egrep command is as follows:

$ egrep [options] pattern [files]

The options parameter is used to specify additional flags that can be used when running the command. These flags can be used to modify the output of the command:

  • pattern: The pattern parameter is used to specify the pattern that is being searched for in the files. It is an extended regular expression and can be used to search for complex patterns in the files.
  • file: The files parameter is used to specify the files that are being searched. This parameter can be omitted if the command is being used to search for patterns in the standard input.

 

egrep Command Options

The egrep command accepts several options. Here are the most commonly-used ones:

  • -i : ignore case
  • -v : invert the search (show lines that do not match the pattern)
  • -l : show only the filenames that contain a match
  • -c : show only the count of matches
  • -n : show the line number of matches
  • -w : match whole words only
  •  

How to install egrep in Different Linux Distributions ?

The egrep command is available in all major Linux distributions. It is included in the default package repositories of most distributions and can be installed using the package manager of the distribution.

To install the egrep command on Ubuntu, use the following command:

$ sudo apt install grep

To install the egrep command on CentOS, use the following command:

$ sudo yum install grep

To install the egrep command on Fedora, use the following command:

$ sudo dnf install grep

 

Examples of egrep Command in Linux

1. Search a File for a Specific Word

Let's say that you have a file called "example.txt" that contains the following text:

This is an example file.

If you want to search the file for the word "example", you would use the following command:

$ egrep example example.txt

This command will search the example.txt file for the word "example" and print any lines that contain it. In this case, it will print out the entire line:

This is an example file.

 

2. Search a Directory for a Specific Word

Let's say that you have a directory called "docs" that contains several text files. If you want to search all of the files in the directory for the word "example", you would use the following command:

$ egrep example docs/*

This command will search all of the files in the "docs" directory for the word "example" and print any lines that contain it.

 

3. Search a Directory for Multiple Words

Let's say that you want to search all of the files in the "docs" directory for the words "example" or "file". You would use the following command:

$ egrep 'example|file' docs/*

This command will search all of the files in the "docs" directory for the words "example" or "file" and print any lines that contain them.

 

4. Find all lines in a file containing the word “linux”:
$ egrep linux file.txt

 

5. Find all lines in a file containing the word “Linux”, ignoring case:
$ egrep -i linux file.txt

 

6. Find all lines in a file NOT containing the word “linux”:
$ egrep -v linux file.txt

 

7. Show only the filenames containing the word “linux”:
$ egrep -l linux *.txt

 

8. Show the count of lines containing the word “linux” in all files in the current directory:
$ egrep -c linux *.txt

 

9. Show the line numbers of lines containing the word “linux” in a file:
$ egrep -n linux file.txt

 

10. Find all lines in a file containing the word “Linux”, matching only whole words:
$ egrep -w linux file.txt

 

11. Find all lines in a file containing the words “Linux” or “Ubuntu”:
$ egrep 'linux|ubuntu' file.txt

 

12. Find all lines in a file containing the words “Linux” and “Ubuntu”:
$ egrep 'linux.*ubuntu' file.txt

 

13. Find all lines in a file containing either the word “Linux” or the word “Ubuntu”, but not both:
$ egrep 'linux.*(ubuntu|linux)' file.txt

 

14. Find all lines in a file containing the word “Linux”, followed by any number of characters, followed by the word “Ubuntu”:
$ egrep 'linux.*ubuntu' file.txt

 

15. Find all lines in a file containing the word “Linux”, followed by any number of non-whitespace characters, followed by the word “Ubuntu”:
$ egrep 'linux\S*ubuntu' file.txt

 

16. Find all lines in a file containing the word “Linux”, followed by any number of characters, followed by the word “Ubuntu”, ignoring case:
$ egrep -i 'linux.*ubuntu' file.txt

 

[Need help in fixing Linux system issues ? We can help you. ]

 



CONCLUSION

This article covers the basics of using the egrep command and some practical examples. Now, you should be able to use the egrep command to search your files and directories quickly and efficiently.


icon

Sign up to Newsletter

...and receive a free support ticket.