15 October 2011

grep command in Linux/Unix

Leave a Comment
grep command in Linux/Unix


                               grep (global regular expression parse) is use for searching a pattern in a files or files . grep scan its input for a pattern and display the lines containing the pattern, line number , files which contain the pattern etc.


syntax:


grep [option] pattern file-name(s)


let we have file  sandeep.lst contains following data
$ cat > sandeep.lst
sandeep | 028 | delhi cantt | 5000
vaibhav | 034 | laxmi nagar | 8000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
SANDEEP|098|rajghat|7000
^C
$


Example


1.  grep "sandeep" sandeep.lst   or   grep sandeep  sandeep.lst     or   grep 'sandeep' sandeep.lst


output:  


$ grep sandeep sandeep.lst
sandeep | 028 | delhi cantt | 5000

$ grep "sandeep" sandeep.lst
sandeep | 028 | delhi cantt | 5000

$ grep 'sandeep' sandeep.lst
sandeep | 028 | delhi cantt | 5000
$




*important pattern can be enclosed within single quotes or double quotes . One thing should be remember that if we are using special symbols (like *,?,[]) then double quotes should be used other wise if single quotes used then these symbol has no meaning.

* if possible always use double quotes.


grep options


1.  ignoring case (-i) :  searches for the pattern by ignoring the case .


Example
  
                       grep -i "sandeep" sandeep.lst


output:  


$ grep -i sandeep sandeep.lst
sandeep | 028 | delhi cantt | 5000
SANDEEP|098|rajghat|7000
$





2. deleting lines (-v) : select those lines which does not contain pattern


Example 


                    grep -v "vaibhav" sandeep.lst


output:  
$ grep -v "vaibhav" sandeep.lst
sandeep | 028 | delhi cantt | 5000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
SANDEEP|098|rajghat|7000
$





3. displaying line number (-n) : This option display the line number of the line containing the pattern.


Example
     
                          grep -n "laxmi nagar" sandeep.lst


output:   
$ grep -n "laxmi nagar" sandeep.lst
2:vaibhav | 034 | laxmi nagar | 8000
7:anki | 051 | laxmi nagar | 5000
$





4. counting lines containing pattern (-c) : This option display the number of times pattern occur.


Example  


                         grep -c "laxmi nagar" sandeep.lst


output: 
$ grep -c "laxmi nagar" sandeep.lst
2
$




if -c option used with multiple files then 

Example  


                     grep -c "sandeep" sandeep*.lst

output: 
$ cat > sandeep2.lst
sandeep
vaibhav
^C

$ grep -c "sandeep" sandeep*.lst
sandeep.lst:1
sandeep2.lst:1
$



*above command searches all the files starting with sandeep in the current directory if that files contains the pattern then it display the result.




5. Matching multiple patterns (-e) : Multiple patterns can be matched using this option.


Example


                         grep -e "sandeep" -e "vaibhav" sandeep.lst


output: 
$ grep -e "sandeep" -e "vaibhav" sandeep.lst
sandeep | 028 | delhi cantt | 5000
vaibhav | 034 | laxmi nagar | 8000
$





grep can be used by combining different options together


Example


                grep -vi  "sandeep" sandeep.lst


*This command output all  lines except lines containing sandeep whether it is in lower case , upper case or mixed.


output:  
$ grep -vi "sandeep" sandeep.lst
vaibhav | 034 | laxmi nagar | 8000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
$


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment