Saturday 28 January 2012

grep


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


Example 1) grep in its simplest form,i:e without any arguments displays  all 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 $ grep  Nokia  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:e  grep –i Nokia  AllPhones.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  -i  Samsung   AllPhones.lst
5

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

Example 3)
You might  be  searching a particular thing excluding a particular word, then use -v   option.
 /home/kapoor $ grep  –v   Samsung   AllPhones.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   you  wish to search more than one keyword   use –e option of grep before each keyword.

/home/kapoor $ grep -e   LG   –e   –i  spice  AllPhones.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  -e   LG   –e   –i  spice  AllPhones.lst

Example 5)
To accomplish  a search within   a 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 –i  nokia   AllPhones.lst  |   grep  -v   –i   Classic
Nokia C2 01 : Rs. 3,799
nokia 7230 : Rs. 5,700

why grep is  such a  great command  in  searching   these results for  you 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  –f   required_phones   AllPhones.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 keyword  use –l option of grep.
The command below lists   all the scripts  which  uses   awk .
/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  *.sh
 pgfile-2.sh:sed -n 1,$p   $h2_file.txt
 pgfile-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 recursively  among  files of directory  and its  sub directories use grep -r .

/home/kapoor $grep  -l  -r  awk   *.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 to  decide on whether the search succeeded, then use grep –q.
.....      #lines of the script
.....      ‘’
grep   -q    win   victory-status.log
if   [   $?  -eq  0  ]
then
 flag=1
else
flag=0
fi
..
..
Here  grep 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.

/home/kapoor $ grep –n  error   status-run.log
40: error  cannot open  file  gt1.txt
160:error  cannot open file    uu6.txt
178:parse  error

2 comments: