Search This Blog

Tuesday, August 17, 2010

Grep Command.

To understand grep We will create a small file as Gandhi.txt.
#touch Gandhi.txt
Above statement will create a file called Gandhi.txt

Post the following lines in a file(Gandhi.txt).
#vi BhagatSingh.txt
press key i for insert copy the text below .and paste it into the file using rightclick
Save the file by pressing esc and then typing :wq

************************************************************
Bhagat Singh, a Sandhu Jat, was born on 28 September 1907 to Kishan Singh and Vidyavati at Chak No. 105, GB, Banga village, Jaranwala Tehsil in the Lyallpur district of the Punjab Province of British India.
In 1923, Bhagat Singh joined the National College in Lahore
Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.
************************************************************

Let us use some grep commands to get answer to following questions .
When was Bhagat Singh born?
What year was Bhagat Singh born?

You know the only file in your system that has any information about Bhagat Singh is BhagatSingh.txt So you type following command to get the birth date of Bhagat Singh.

# grep born BhagatSingh.txt
Bhagat Singh, a Sandhu Jat, was born on 28 September 1907 to Kishan Singh and Vidyavati at Chak No. 105, GB, Banga village, Jaranwala Tehsil in the Lyallpur district of the Punjab Province of British India.

Now lets print all lines from files with word Bhagat in it.
# grep bhagat BhagatSingh.txt
Surprised of getting nothing as output.
Well this is because grep without -i option ,considered case sensitive.
In our example we have used word gandhi as search parameter.
Now try the same with Gandhi word.(Notice capital letter G).

# grep Bhagat BhagatSingh.txt
Bhagat Singh, a Sandhu Jat, was born on 28 September 1907 to Kishan Singh and Vidyavati at Chak No. 105, GB, Banga village, Jaranwala Tehsil in the Lyallpur district of the Punjab Province of British India.
In 1923, Bhagat Singh joined the National College in Lahore
Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.

This time Mission Successful. :)

I know what you might be thinking at this stage.
Do i always need to remember the exact Case while searching through files.
Well the answer is No*.
(*:Provided you use option i)

Lets try the same example with word gandhi (Notice all small letters).
# grep -i bhagat BhagatSingh.txt
Bhagat Singh, a Sandhu Jat, was born on 28 September 1907 to Kishan Singh and Vidyavati at Chak No. 105, GB, Banga village, Jaranwala Tehsil in the Lyallpur district of the Punjab Province of British India.
In 1923, Bhagat Singh joined the National College in Lahore
Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.

-i option is used to ignore cases.


Lets assume you know the birth date of Bhagat Singh and more interested in knowing other facts about him.
Following command will help you get other information .

below command will display all lines except the lines containing born word in it.

# grep -v born BhagatSingh.txt
In 1923, Bhagat Singh joined the National College in Lahore
Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.

-v is a option used to accomplish such tasks.

Other usage.
-n to display line number.
# grep -vn born BhagatSingh.txt
2:In 1923, Bhagat Singh joined the National College in Lahore
3:Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
4:The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:
5:    A reign of terror in the city of Cawnpore in the United Provinces and an attack on Mahatma Gandhi by a youth outside Karachi were among the answers of the Indian extremists today to the hanging of Bhagat Singh and two fellow-assassins.

-e
Incase you dont want to read anything about his birth and hanging you may use below command.
# grep -vn -e born -e hanging BhagatSingh.txt
2:In 1923, Bhagat Singh joined the National College in Lahore
3:Bhagat Singh, Rajguru and Sukhdev were sentenced to death in the Lahore conspiracy case and ordered to be hanged on 24 March 1931.
4:The execution of Bhagat Singh, Rajguru and Sukhdev were reported widely by the press, especially as they were on the eve of the annual convention of the Congress party at Karachi. Gandhi faced black flag demonstrations by angry youths who shouted "Down with Gandhi". The New York Times reported:


-c  To count the number of matches
# grep -vi -c born BhagatSingh.txt
4


There are many other usage of grep which can be included but the main purpose of this article was to make you aware of its common and widely used syntax.
a good article over basic grep command can be found here.
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
Expressions that can be used with greps are explained here.
http://www.thegeekstuff.com/2011/01/regular-expressions-in-grep-command/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+TheGeekStuff+%28The+Geek+Stuff%29

No comments:

Post a Comment