Saturday, 28 January 2012

grep

grep

grep is a saviour command forunixusers. grep can be used to search for patterns in a file or standard input. The pattern search includes findingthe line number of the keyword, counting the number of occurrences of a keywordand many more. we will see in the following examples.

Example 1) grep in its simplest form,i:e without any arguments displaysall the lines where the pattern occurs.

/home/kapoor $ cat AllPhones.lst

Samsung C5010 Squash : Rs. 3,245

Samsung C5130 : Rs. 3,500

LG GU285 : Rs. 3,750

Nokia C2 01 : Rs. 3,799

Nokia 2730 Classic : Rs. 3,960

INQ Mini 3G : Rs. 4,000

Spice G6500 : Rs. 4,295

Sony Ericsson Cedar : Rs. 5,100

Samsung Star Nano 3G S3370 : Rs. 5,100

Samsung L700 : Rs. 5,300

Spice QT95 : Rs. 5,500

nokia 7230 : Rs. 5,700

Sony Ericsson J105 Naite : Rs. 5,800

Samsung Metro 3G S5350 : Rs. 6,000

/home/kapoor $ grepNokia AllPhones.lst

Nokia C2 01 : Rs. 3,799

Nokia 2730 Classic : Rs. 3,960

To make case insensitive search use grep with –i option

i:egrep –i NokiaAllPhones.lst

now the result would also include the line nokia 7230 : Rs. 5,700

Example 2)

It might be required for you to count the number of occurrences of a particular keyword, use -c option .

/home/kapoor $ grep-c-iSamsungAllPhones.lst

5

Remember always to use –i just before the search keyword.

Example 3)

You mightbesearching a particular thing excluding a particular word, then use -voption.

/home/kapoor $ grep–vSamsungAllPhones.lst

LG GU285 : Rs. 3,750

Nokia C2 01 : Rs. 3,799

Nokia 2730 Classic : Rs. 3,960

INQ Mini 3G : Rs. 4,000

Spice G6500 : Rs. 4,295

Sony Ericsson Cedar : Rs. 5,100

Spice QT95 : Rs. 5,500

nokia 7230 : Rs. 5,700

Sony Ericsson J105 Naite : Rs. 5,800

Example 4)

When youwish to search more than one keyworduse –e option of grep before each keyword.

/home/kapoor $ grep -eLG–e–ispiceAllPhones.lst

LG GU285 : Rs. 3,750

Spice G6500 : Rs. 4,295

Spice QT95 : Rs. 5,500

Similarly if you require to search for all other phones other than LG and spice use

grep-v-eLG–e–ispiceAllPhones.lst

Example 5)

To accomplish a search withina searched line use pipes. suppose you are planning to buy

a nokia phone other than a classic one, use grep as shown

/home/kapoor $ grep –inokiaAllPhones.lst|grep-v–iClassic

Nokia C2 01 : Rs. 3,799

nokia 7230 : Rs. 5,700

why grep issuch a great command insearchingthese results foryou is that it does them amazingly fast.

Example 6)

You have seen in Example 4) how multiple keyword search was done using –e option. but it becomes very lengthy and untidy to use –e after each keyword if there are 100s of things to be searched.

In such cases you can use grep with-f<file> option.the below lines show how.

Save the list of phones in a file.

/home/kapoor $cat >required_phones

Sony Ericsson

INQ

Nokia

^D

/home/kapoor $grep–frequired_phonesAllPhones.lst

Nokia C2 01 : Rs. 3,799

Nokia 2730 Classic : Rs. 3,960

INQ Mini 3G : Rs. 4,000

Sony Ericsson Cedar : Rs. 5,100

Sony Ericsson J105 Naite : Rs. 5,800

Similarly you can use –v–f<file>to display all the lines except those in the <file>

The situation is like this. You have a key word and you want to search it in a large number of

files in a path. The following examples (7-9)show you various techniques and scenarios.

Example 7)

To give only a list of files which contain your keyworduse –l option of grep.

The command below listsall the scriptswhichusesawk .

/home/kapoor $grep-l“awk”*.sh

Pattern-gen.sh

Large-box.sh

Example 8)
If grep is used to search in a group of files for a pattern, it gives the following default output./home/kapoor $grep sed *.shpgfile-2.sh:sed -n 1,$p $h2_file.txtpgfile-2.sh:sed 's/;//g' $h3_file.txt
daily-b1.sh:sed 's/-/_/g' prime_f.js

If you do not want grep to display the searched filenames and only want the patterns use grep -h grep -h sed *.sh gives you the output.
sed -n 1,$p $h2_file.txt sed 's/;//g' $h3_file.txt sed 's/-/_/g' prime_f.js

Example 9)

To search for patterns recursivelyamongfiles of directory and its sub directories use grep -r .

/home/kapoor $grep-l -rawk*.sh

Pattern-gen.sh

Large-box.sh

Shell1/getfiles_2.sh

Shell1/remainder.sh

Shell1/byte-logs.sh

Shell1/new-scripts/byte-logs.sh

Shell2/vault-value.sh

Example 10)

In shell scripts you may need to search for a pattern but do not want the command to write to

Standard output but you need todecide on whether the search succeeded, then use grep –q.

.....#lines of the script

.....‘’

grep-qwinvictory-status.log

if[$?-eq0]

then

flag=1

else

flag=0

fi

..

..

Heregrep does not give any output, but the exit condition is 0 if search is found and non zero if not found ( $?Stores the exit condition of the output of previous command.)

Example 11)

You can also know the line numbers of the searched patterns through grep by using –n command.