20 Examples of Useful egrep Commands in Linux

Linux

 

Summary: In this tutorial, we will discuss some practical examples of the egrep command. After following this tutorial, users should be able to perform text searches more efficiently in Linux.

Have you ever been frustrated because you couldn’t find the necessary information in the logs? Extracting the necessary information from a large data set is a complex and time-consuming task.

Things get really difficult if the operating system doesn’t provide the right tools and this is Linux to your rescue. Linux provides various text filtering utilities such as awk, attractive, cutet cetera.

However, egrep is one of the most powerful and most commonly used utilities for word processing in Linux, and we will discuss some examples of these. egrep request.

The egrep commands in Linux are recognized by them grep command, which is used to search and match a specific pattern in the file. It works similar to grep -E (grep Extended regex), but it mainly looks for a specific file or even lines to stack or print lines in the given file.

 

Syntax of egrep command as follows:

$ egrep [OPTIONS] PATTERNS [FILES]

Let’s create a sample text file with the following contents as an example:

$ cat sample.txt
Sample text file

Here, we can see that the text file is ready. Now let’s discuss a few common examples that can be used every day.

1. How to find a pattern in a single file

Let’s start with a simple pattern matching example where we can use the below command to search for a string professional in one sample.txt file:

$ egrep professionals sample.txt
Searching for strings in files
Searching for strings in files

Here, we can see that the command prints the line containing the specified pattern.

2. How to highlight matching patterns in files

We can make the output more informative by marking the pattern accordingly. To achieve this we can use --color ‘s option egrep request. For example, the below command will highlight the text professionals red:

$ egrep --color=auto professionals sample.txt
Highlight text in files
Highlight text in files

Here, we can see that the same output has more information than the previous output. In addition, we can easily determine that the word professionals is repeated twice.

On most Linux systems, the above setting is enabled by default using the following alias:

$ alias egrep='egrep –color=auto'

3. How to find a pattern in multiple files

The egrep command accepts multiple files as an argument, allowing us to search for a specific pattern in multiple files. Let’s understand this with an example.

First, make a copy of sample.txt file:

$ cp sample.txt sample-copy.txt

Now look for the pattern professionals in both files:

$ egrep professionals sample.txt sample-copy.txt 
String search in multiple files
String search in multiple files

In the above example, we can see the filename in the output, which represents the matching line from that file.

4. How to count matching lines in file

Sometimes we just need to find out if the pattern is in the file or not. If yes, how many lines does it currently have? In such cases, we can use -c command options.

For example, the command below will display first as an output because the word professionals only in one line.

$ egrep -c professionals sample.txt

1

5. How to print only matching lines in the file

In the previous example, we saw that -c the option does not count the number of occurrences of the pattern. For example, the word professionals appear twice on the same line but -c optionally treat it as a single match.

In such cases, we can use -o command’s option to print only the matching pattern. For example, the command below will display the word professionals on two separate lines:

$ egrep -o professionals sample.txt

Now count the lines using wc . command:

$ egrep -o professionals sample.txt | wc -l
Print the matching lines in the file
Print the matching lines in the file

In the example above, we used a combination of egrep and WC commands to count occurrences of a particular pattern.

6. How to find a pattern by skipping case

By default, egrep perform pattern matching in a case-sensitive manner. It means the words – we, We, wE and WE are considered different words. However, we can enforce a case-insensitive search using -i right to buy.

For example, in the command sample below, the match will succeed for text we and We:

$ egrep -i we sample.txt 
The search string is case-insensitive
The search string is case-insensitive

7. How to Exclude Partially Matching Patterns

In the previous example, we saw that egrep partial match execution. For example, when we search for text we then pattern matching also succeeds for other texts. Such as web, website and okay.

To overcome this limitation, we can -w This option enforces whole-word matching.

$ egrep -w we sample.txt
Print only matching patterns in the file
Print only matching patterns in the file

8. How to reverse pattern matching in file

So far, we have used egrep command to print lines with the given pattern. However, sometimes we want to perform the operation in the opposite way.

For example, we may want to print lines where the given pattern is not present. We can achieve this with the help of -v right to buy:

$ egrep -v we sample.txt
Reverse the matching pattern in the file
Reverse Pattern Match in File

Here we can see that the command to print all lines contains no text we.

9. How to Find the Line Number of a Pattern

We can use -n command’s option to enable line numbering, showing line numbers in output when pattern matching is successful. This simple trick makes the output more meaningful.

$ egrep -n professionals sample.txt
Number of printed lines of matte pattern
Line number of the matching pattern

In the above output, we can see that from professionals present in day 5 row.

10. How to Perform Pattern Matching in Quiet Mode

In quiet mode, egrep command does not print matching pattern. So we have to use the return value of the command to determine if the pattern matching was successful.

We can use -q command’s option to enable silent mode, very useful while writing shell scripts.

$ egrep -q professionals sample.txt
$ egrep -q non-existing-pattern sample.txt
Search Matching Quiet Mode
Search Matching Quiet Mode

In this example, zero a return value indicates the presence of the sample while a non-zero value indicates the absence of the sample.

11. How to display lines before pattern matching

Sometimes it’s a good idea to show a few lines around the matching pattern. For such situations we can use -B command’s option, show WOMEN line before the matching pattern.

For example, the command below will print the line where the pattern matches successfully as well as the 2 lines before it.

$ egrep -B 2 -n professionals sample.txt 
Print the line before pattern matching
Print the line before pattern matching

In this example, we used -n option to display line numbers.

12. How to display lines after pattern matching

In the same way, we can use -A command’s option to display lines after pattern matching. For example, the command below will print the line where the pattern matches successfully as well as the next 2 lines.

$ egrep -A 2 -n professionals sample.txt
Print the line after pattern matching
Print the line after pattern matching

13. How to show lines around pattern match

Besides this, egrep support command -C options combine the functions of options -A and -Bshows the lines before and after the matching pattern.

$ egrep -C 2 -n professionals sample.txt 
Print the line around the pattern match
Print the line around the pattern match

14. How to find a pattern in multiple files recursively

As discussed before, we can perform pattern matching across multiple files. However, the situation gets complicated when the files are present under multiple subdirectories and we pass them all as command arguments.

In such cases we can perform pattern matching recursively using -r as in the following example.

First, create 2 subfolders and copy sample.txt submit to them:

$ mkdir -p dir1/dir2
$ cp sample.txt dir1/
$ cp sample.txt dir1/dir2/

Now, let’s do the search recursively:

$ egrep -r professionals dir1
String recursive search in file
String recursive search in file

In the above example, we can see that the pattern match was successful for dir1 / dir2 / sample.txt and dir1 / sample.txt files.

15. How to match a single character using a regular expression

We can use a dot (.) to match any single character except the end-of-line character. For example, the below regular expression matches the text har, hat and yes:

$ egrep "ha." sample.txt
Match a character in a file
Match a character in a file

16. How to match 0 or more character occurrences

We can use asterisk (*) to match zero or more occurrences of the previous character. For example, the regular expression below matches text containing a string we followed by zero or more occurrences of the character b.

$ egrep "web*" sample.txt
Match string in text file
Match string in text file

17. How to match one or more occurrences of the previous character

We can use the plus sign (+) to match one or more occurrences of the previous character. For example, the regular expression below matches text containing the string we followed by at least one occurrence of the character b.

$ egrep "web+" sample.txt
Match string by character in file
Match string by character in file

Here we can see that the pattern match fails for words – we and weredue to no characters b.

18. How to match the beginning of a line

We can use caret (^) represents the beginning of the line. For example, the regular expression below prints lines starting with the text We:

$ egrep "^We" sample.txt
Print text that matches the starting line
Print text that matches the starting line

19. How to match end of line

We can use the dollar ($) to represent the end of a line. For example, the regular expression below prints lines ending in text e.:

$ egrep "e.$" sample.txt
Print text that matches the ending line
Print text that matches the ending line

20. How to remove blank lines in files

We can use caret (^) immediately followed by the dollar ($) to represent blank lines. Let’s use this in a regular expression to remove blank lines:

$ egrep -n -v "^$" sample.txt
Remove blank lines in file
Remove blank lines in file

In the above output we can see that line numbers 2, 4, 6, 8 and 10 are not displayed because they are empty.

Inference

In this article, we have discussed some useful examples of egrep commands. One can use these examples in daily life to improve productivity.

Do you know any other best examples of egrep command in Linux? Let us know your views in the comments section below.

Hope this helps!

Source link

Share: