Pages

Learn Linux-10 grep

AIm: To introduce th reader to the grep command and its various uses.

grep:

grep is used for searching patterns in a given file.
grep stands for Global Regular Expression Print, there seem to a lot of other full forms that are floating around in the net. But the working of grep fits perfectly to this acronym.

syntax: grep [options]  "pattern" filename 

The pattern could be any thing, strings, numbers mix of both etc.

grep by default, with out any options, will print the whole line that has the "pattern" present in it. Even if the "pattern" is a substring .

For eg:
user@Desktop:~/folder1$ grep "the" file1
creating the first file in vi editor
There are other editors that can also be used.
user@Desktop:~/folder1$

The contents of the file were

user@Desktop:~/folder1$ cat file1
creating the first file in vi editor
VI is a widely used editor.
There are other editors that can also be used.
user@Desktop:~/folder1$

Out of the three lines in the file, the second line does not have the word "the", hence the output of the command did not show the second line.
In the third line the word "the" is present as a substring of "There".

Note that by default "grep" is case sensitive.

grep comes with a huge number of options, which is why it is one of the most popular command in the world of Linux. You can have a  look at the options using the man command.

Let us look at some of the options and how to use them.

option:  -c 

-c is used to get a count of the number of lines that have the  pattern in them, Note it give the number of lines not the number of times tha pattern occurs.

To explore the options further, let us create a file with the following text in it.

Sharma always quarreled with his neighbours.
Not because he did not like them, but because he wanted to shout. That is strange isn't it. You fight because you want to shout. So one day I asked him, why do you keep fighting, if you feel like fighting. Why don't you just shout in your home. He said I tried doing that and they admitted me to THE mental hospital.

I have named the file as grep_example.

for eg:
user@Desktop:~/folder1$ grep -c you grep_example
1
user@Desktop:~/folder1$

Even though the word "you" appears twice in the secong line the output count is only "1" because both the "you" are on the same line and by default "-c" counts the number of lines that have the pattern.

option : -o

The "-o" option is used when you don't want to view the whole line but only the pattern that has matched.

For eg:
user@Desktop:~/folder1$ grep -o  the grep_example
the
the
user@Desktop:~/folder1$

We get the output "the" three times. That is because the pattern "the" is present in the input file three times.
once in the word "them". Once in the word "they" and once as the word "the" itself.

Try it out :
Can you used the "-c" and "-o" together such that get the exact count of number of times the pattern occurs instead of just the number of lines that have the pattern. Hint: You will have to use the "|" symbol that we learnt during the output redirection.

option: "-w"

The option "-w" is used when we want to match the pattern exactly, unlike the previous case where even if "the" was present as a substring it was being counted as a match. On the ohter hand if we want to match only if there is the word "the" only.

for eg:
user@Desktop:~/folder1$ grep -w the grep_example
that and they admitted me to THE mental hospital.
user@Desktop:~/folder1$

As compared to when we run the grep command with out the "-w" option, the output in this case in only the line that has the word "the" and does not list the lines that have "the" as a substring.

Option : -i

This option when we want our search to ignore the case, and search only for the matching string irrespective of the case.
For eg:

user@Desktop:~/folder1$ grep -o -i "the" grep_example
the
the
THE
user@Desktop:~/folder1$

We can see that the output also has "THE" in uppsercase too, which was not present when we searche for the same string with out the "-i" option.

option: -v

This option is used when we want to ouput all the lines that in a file that do not have the search pattern.

For eg:

user@Desktop:~/folder1$ grep -v you grep_example
sharma always quarreled with his neighbours. Not because he did not like them,that and they admitted me to THE  mental hospital.
user@Desktop:~/folder1$

The output only has the lines that do not have the string "you" in it.


option: -n

The option "-n" is used in case we want the ouput lines to be prefixed with their line numbers.

For eg:

user@Desktop:~/folder1$  grep -n "the" grep_example
1:sharma always quarreled with his neighbours. Not because he did not like them,
5:that and they admitted me to THE  mental hospital.
user@Desktop:~/folder1$

The number prefixed before the lines are the line numbers on which the matching pattern occurs.

Try it out: 
1. Use the grep options to get a count of number of lines that do not have the search pattern.


Refferences:
In case you want to learn grep in more detail you can refer to
Grep Pocket Reference

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete