grep, egrep and fgrep tutorial

grep, egrep and fgrep

This will be a nugget on usage of grep, egrep and fgrep. Lets quickly breeze through their usage with some basic examples.

Grep

Grep is one of the most commonly used command in conjunction with piping for anyone who works with Linux based systems. Grep is majorly used in parsing files / standard input by matching the given pattern and displaying the matching lines.

 

Examples for grep

Consider the file file.txt contains the below content. We’ll be using this content all through our example. If possible, copy it in a notepad so that it’ll be easy for you to refer instead of scrolling up every time when we discuss about different examples.

# cat file.txt

devopsideas - all about devops

Learn things step by step at Devopsideas

Subscribe to devopsideas

devopsideas to your rescue

123 8 9 10

 

  • grep ^devopsideas file.txt
# grep ^devopsideas file.txt
 
devopsideas - all about devops
devopsideas to your rescue

 

The above command will display the lines that start with the word devopsideas. ‘^’ is used to indicate “starts with” in regex

 

  • grep -c ^devopsideas file.txt
# grep -c ^devopsideas file.txt
2

The same command with -c flag will give the count. In our example, 2 lines start with the word devopsideas. Hence we get the count as two

 

  • grep -c devopsideas$ file.txt
# grep -c devopsideas$ file.txt
1

 

Above command will list the lines that end with devopsideas. You can notice one of the line ends as Devopsideas. This will not be counted since we did not pass the case insensitive flag ‘-i’ as a result of which we get the count as 1

 

# grep -ci devopsideas$ file.txt
2

The same command with the additional flag -i (insensitive) resulting in the count to be 2.

 

Next we can see some example that deals with character reference

  • grep [z] file.txt
# grep [z] file.txt

 

The above command checks for the presence of the character ‘z’ in the file. It returns empty since there is no word that contains the character ‘z’.

 

  • grep [h] file.txt
# grep [h] file.txt
Learn things step by step at Devopsideas

Same as previous example and this time it returns the line which contains the character ‘h’

 

Try it yourself

Try the below patterns and see what you get

  • grep [dezoqp] file.txt
  • grep ^[dezoqp] file.txt
  • grep [a-e] file.txt
  • grep [1-9] filt.txt

 

You can pass list of patterns in a file.

  • grep -f pattern.txt test.txt
# cat pattern.txt
[3-8]
Devopsideas

# grep -f pattern.txt file.txt
Learn things step by step at Devopsideas
123 8 9 10

We have given  the pattern list in the file pattern.txt and then ran grep command against it with ‘-f’ flag. We can make use of this approach when we frequently use complex patterns. Instead of typing it, we can put it in a file and reuse it.

 

If you are not sure in which file the content that you are searching for resides (like heap size, password etc), then you can make use of the below command. This is one really useful command which will save your time. For example, if you want  the files that contains the name ‘devops’ under your home directory, then you can execute the below command

  • grep -lri devops ~
# grep -rli devops ~
/root/file.txt
/root/pattern.txt

 

The flag -r represent recursive, -l represents list and -i for insensitive. If you execute the above command without -l flag, then it’ll also list the contents inside the files matching the pattern.

# grep -ri devops ~
/root/file.txt:devopsideas - all about devops
/root/file.txt:Learn things step by step at Devopsideas
/root/file.txt:Subscribe to devopsideas
/root/file.txt:devopsideas to your rescue
/root/pattern.txt:Devopsideas

 

Egrep

Egrep stands for grep with extended regular expression. It is same as using grep with -E flag (grep -E) and you can make extensive use of regex with egrep.

Let us see few examples for egrep.

  • egrep ‘Learn.*Devopsideas’ file.txt
  • egrep ‘Learn.*Subscribe’ file.txt
# egrep 'Learn.*Devopsideas' file.txt
Learn things step by step at Devopsideas

# egrep 'Learn.*Subscribe' file.txt

The first statement in the above example will check for the word “Learn” and “Devopsideas” and will list if it matches the pattern. In the above example, “.*” stands for AND. The second statement will not return anything since the pattern “Learn” and “Subscribe” are not found in the same sentence.

 

  • egrep ‘Learn|Subscribe’ file.txt
# egrep 'Learn|Subscribe' file.txt
Learn things step by step at Devopsideas
Subscribe to devopsideas

The above command searches for "Learn" OR "Subscribe" same as Logical OR. The output is as expected.

 

  • egrep -v  ‘Devopsideas|Subscribe’ file.txt
# egrep -v  'Devopsideas|Subscribe' file.txt
devopsideas - all about devops

devopsideas to your rescue

123 8 9 10

The “-v” flag in the above command will negate the specific pattern that we are matching.

 

Fgrep

Fgrep stands for grep with fixed strings. It treats the patterns that we are passing as fixed strings and does not interpret it as regular expressions. Let us see this with some examples.

Consider the file.txt contains the below content

# cat file.txt
devopsideas - all about devops

Learn things step by step at $Devopsideas

Subscribe to devopsideas$

^devopsideas to your rescue

Subscribe|Unsubscribe

 

  • fgrep ^devopsideas file.txt
# fgrep ^devopsideas file.txt
^devopsideas to your rescue

As you can see from the example, fgrep does not interpret “^devopsideas” as pattern that checks for word starting as devopsideas. Instead it checks for the fixed pattern “^devopsideas” and displays the result.

 

  • fgrep -i ‘subscribe|unsubscribe’ file.txt

# fgrep -i ‘subscribe|unsubscribe’ file.txt
Subscribe|Unsubscribe

As expected, it is directly searching for patterns in fixed mode without interpreting it. egrep would have have also listed the line “Subscribe to devopsideas$” since it’ll treat ‘|’ as Logical OR whereas fgrep treats it as fixed character.

 

So far we have seen some basic usages of grep, egrep and fgrep. They can be used more effectively and I hope this article gave you some basic understanding of using different types of grep.