Chat Zalo Chat Messenger Phone Number Đăng nhập
Linux egrep Command with Examples

Linux egrep Command with Examples

The egrep command belongs to the grep command family that is used for pattern search in Linux. If you used the grep command, egrep works the same as grep -E (grep Extended regex’). Egrep scans a specific file, line by line, and prints the lines containing the regular search/expression string. In this article, we will explain 15 useful examples of egrep commands that will help newbies and even experts perform meaningful searches on LinuxWe have made these examples on a Debian 10 Buster system, but they can be easily replicated on most Linux distributions.

Example 1: Find a specific string in a file

This is the most common use of the egrep command. What you need to do is specify a string that you want to search for and the file name that you want to search for that string. The result then displays the entire line containing the searched string.

Syntax

:

Example:

In this example, I searched for the word “debian” in the specified text file. You can see how the results show the entire line containing the word “debian

“:

1 20

Example 2: Find

a specific string in multiple files With the

egrep command, you can search for a string among multiple files that reside in the same directory. You just have to be a little more specific by providing a “pattern” for the searched files. This will be made clearer by the example we will present.

Syntax

:

Example: Here

, we will search for the word “debian” in all .txt files by specifying the filename pattern as follows:

The command has printed all the lines, along with the filenames containing the word “debian” of all the files .txt in the

current directory. Example

3: Recursive Directory String

Search

When you want to search for a string in all files in a directory and its subdirectories, you can do so by using the -r flag with the egrep command.

Syntax

: Example: In this example, I am searching for the word “sample” in the files of the entire current directory (Downloads). The results contain all the

lines, along with the file names, that contain the word “sample” for all the files in the Downloads directory and its subdirectories.

Example 4: Perform a case-insensitive

search

With the -i flag, you can use the egrep command to print results based on a search string without having to worry about your case.

Syntax

:

Example:

Here, I am searching for the word “debian”

and I want the results to show all the lines in the file that contain the word “debian” or “Debian”, regardless of your case.

4 18

You can see how the -i indicator helped me find all the lines containing the search string through a “case-insensitive” search

.

Example 5: Find a string as a whole word and not

as a substring

When you normally search for a string through egrep, you print all the words that contain the string as a substring. For example, searching for the string “on” will print all words containing the string “on” such as “on”, “only”, “monitor”, “clone”, etc. If you want the results to show only the word “on” as a complete word and not as a substring, you can use the -w flag with egrep.

Syntax

:

Example

:

Here I am looking for the string “on” in a sample file:

5 18

You can see in the output above, which also contains the word “only”. However, this is not what I want, as I am searching exclusively for the word “on”. So, here’s the command I’ll use instead

:

6 16

Now my search results only include the lines that contain the word “

on” as a whole word

. Example 6: Print only the file names that contain the string Sometimes we only want to get the

file names

that

contain a specific string, instead of printing the lines that contain it. This can be done using the -l flag (lowercase L) with the egrep command.

Syntax

: Example:

Here I am looking for the string “sample” in all .txt files in the current directory

: The search results only

print the name of the files that contain the

specified string.

Example 7: Print only the search string from a file

Instead of printing

the

entire line that contains the search string, you can use the egrep command to print the string itself. The string will be printed the number of times it appears in the specified file.

Syntax

:

Example

:

In this example, I’m looking for the word “This” in my file

.

Note: This use of the command is useful when searching for a string based on a regular expression pattern. We will explain regular expressions in detail in one of the following examples.

Example 8: Show n number of lines before, after, or around the search

string Sometimes it is

very important to know the context in a file where a specific string is used. The egrep is useful in the sense that it can be used to display the line containing the search string and also a specific number of lines before, after, and around it.

This is the sample text file that I will use to explain the following examples

: N number of lines After the search string: Using the mark A as follows will display the line containing the search string and N number of lines after it:

Example

:

N number of lines Before the search

string

:

Using flag B as follows will display the line containing the search string and N number of lines before it:

Example

: N number of lines Before search string: Using flag C as

follows will display the line containing the search string and N number of lines before and after it

:

Example:

12 9

Example

9: Matching regular expressions in files The egrep

command becomes more efficient as you search for regular expressions instead of solid search strings in a file.

Syntax

:

Let us explain how you can use regular expressions in your search egrep:

Repeat operator Use ?

The previous point before ? is optional and matches at most

once * The previous element before * will match zero or more times + The previous element before + will match one or more times {n} The previous element matches exactly n number of times. {n,} The previous element matches n or more times {,m} The previous element matches maximum m times {n,m} The previous element matches at least n times, but not more than m times Example: In the following

example, the lines that contain the following expression match

: starting with “

Gnome” and ending with “programs”

Example

10: Highlight

the search

string When you set the environment variable GREP_OPTIONS as shown below, you get its output with the search string/pattern highlighted

in the results:

You can then search for the string in any way we’ve described in the examples in this article.

Example 11: Perform reverse lookup on a file

By reverse lookup, we mean that the egrep command prints everything in the file, except the lines that contain the search string. We will use the following example file to explain reverse lookup through egrep. We have printed the contents of the file using the command cat

:

14 7

Syntax

:

Example: From

the sample file we mentioned, we want to omit the line containing the word “two” in the output

, therefore, we will use the following command:

You can see how the output contains everything from the sample file except the second line that contained the search string “dos”.

Example 12: Perform reverse lookup based on multiple search criteria/patterns

With the -v flag, you can also have the egrep command perform a reverse search based on more than one search string/pattern

.

We’ll use the same sample file that we mentioned in Example 11 to explain this scenario.

Syntax

:

Example: From the example file we mentioned

, we want to omit the lines containing the words “one” and “two” in the output, therefore, we will use the following command:

We have provided two words to omit using the -e flag, therefore the output will appear as follows

:

Example 13: Print the number of lines that match the string

Instead of printing the searched string from the file or the lines that contain it, you can use the egrep command to count and print the number of lines that match the string. This count can be obtained using the -c flag with the egrep command.

Syntax

: Example:

In this example, we will use the -c flag

to count the number of lines that contain the word “This” in our example file:

You can also use the reverse lookup function here to count and print the number of lines that do not contain

the search string:

Example

14: Display the line number where it matches

the string With the -n flag, you can have the egrep command print the matching line along with the line number that contains the search string.

Syntax

: Example:

You can see how line numbers are displayed in search results

. Example 15: Display the position in the file where the search string matches

If you want to know the position in the file where the search string exists, you can use the -b flag with the egrep command

.

Example:

Search results print the byte offset of the file where the search word exists.

This was a detailed use of the egrep command. By using a combination of the indicators explained in this article, you can perform more meaningful and complex searches in the archives.

Contact US