Nov 25, 2012

Unix Grep Examples


Grep
######

1) How to exclude lines in a grep

syntax: grep -v <pattern> <filename>

e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA

>> grep -v "prabhath" test.txt

It will exclude the lines which contain "prabhath"

o/p:
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA


2) How to exclude lines in a grep with ignore case

syntax: grep -iv <pattern> <filename>

e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA

>> grep -iv "prabhath" test.txt

It will exclude the lines which contain "prabhath", "Prabhath", "PRABAHTAH" etc.,

o/p:
ramesh
vamsi
lakshmi muvvala
LAKSHMI MUVVALA

3) How to exclude lines which contain multiple patterns at one shot

Syntax: grep -v -e <pattern> -e <pattern> <filename>

e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA


>>grep -v -e "prabhath" -e "lakshmi" test.txt

It will exclude the lines which contains the pattern "prabhath" & pattern "lakshmi"

o/p:
ramesh
vamsi
PRABHATH
LAKSHMI MUVVALA


>>grep -iv -e "prabhath" -e "lakshmi" test.txt

Please note "i" - ignore case
It will exclude the lines which contains the pattern "prabhath", "PRABHATH" & pattern "lakshmi", "LAKSHMI"

o/p:
ramesh
vamsi


4) Counting the number of matches using grep -c

Syntax: grep -c "pattern" filename

e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA

>>grep -c "prabhath" test.txt
1

>>grep -ic "prabhath" test.txt
2


5) Counting the number of lines that does not match the pattern

e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA

>>grep -vc prabhath test.txt
5

(or)

>>grep -v -c prabhath test.txt
5


6) Display N lines around match (Before & After)

Syntax: > grep -C 2 "Pattern" File

e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA


>> grep -C 2 "lakshmi" test.txt

o/p:
ramesh
vamsi
lakshmi muvvala //Actual Match
PRABHATH
LAKSHMI MUVVALA


7) Display N lines Before Match

Syntax: > grep -B 2 "Pattern" File

e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA

>> grep -B 2 "lakshmi" test.txt

o/p:
ramesh
vamsi
lakshmi muvvala //Actual Match


8) Display N lines After Match

Syntax: >> grep -A 2 "Pattern" File

e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA

>> grep -A 2 "lakshmi" test.txt

o/p:
lakshmi muvvala //Actaul Match
PRABHATH
LAKSHMI MUVVALA


9)  Checking for full words, not for sub-strings using grep -w

e.g.,
>> cat test.txt
prabhath
prabhathkota
prabhath kota
ramesh
vamsi
lakshmi muvvala
PRABHATH
PRABHATHKOTA
PRABHATH KOTA
LAKSHMI MUVVALA

Syntax: >> grep -w "Pattern" File

>>grep -iw "kota" test.txt

o/p:
prabhath kota
PRABHATH KOTA


>>grep -i "kota" test.txt

o/p:
prabhathkota
prabhath kota
PRABHATHKOTA
PRABHATH KOTA


10) How to get line numbers in grep

>> cat test.txt
prabhath
prabhathkota
prabhath kota
ramesh
vamsi
lakshmi muvvala
PRABHATH
PRABHATHKOTA
PRABHATH KOTA
LAKSHMI MUVVALA


>> grep -n 'PRABHATH' test.txt

o/p:
7 PRABHATH
8 PRABHATHKOTA
9 PRABHATH KOTA

>> grep -in 'PRABHATH' test.txt

o/p:
1 prabhath
2 prabhathkota
3 prabhath kota
7 PRABHATH
8 PRABHATHKOTA
9 PRABHATH KOTA


11) Grep Pipe Examples

1) Display cpu model name

#With Pipe
]# cat /proc/cpuinfo | grep -i 'Model'

#Without Pipe
]# grep -i 'Model' /proc/cpuinfo    

2) grep dbname *.dbl | grep mysql


12) Some complex examples using Grep...

>> cat test.txt
prabhath
prabhathkota
prabhath kota
ramesh
vamsi
lakshmi muvvala
PRABHATH
PRABHATHKOTA
PRABHATH KOTA
LAKSHMI MUVVALA


>> grep -nC 2 'lakshmi' test.txt

Note: "-C 2" will grep for 2 lines above and 2 lines below
          "-n" will give the line numbers

o/p:
4 ramesh
5 vamsi
6 lakshmi muvvala
7 PRABHATH
8 PRABHATHKOTA


13) Grep OR

1) grep 'pattern1\|pattern2' filename

$ cat employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   Manager    Sales       $6,000


$ grep 'Tech\|Sales' employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
500  Randy   Manager    Sales       $6,000

2) grep -E 'pattern1|pattern2' filename

$ grep -E 'Tech|Sales' employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
500  Randy   Manager    Sales       $6,000


3) egrep 'pattern1|pattern2' filename

$ egrep 'Tech|Sales' employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
500  Randy   Manager    Sales       $6,000


14) Grep AND

grep -E 'pattern1.*pattern2' filename
grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

$ grep -E 'Dev.*Tech' employee.txt
200  Jason   Developer  Technology  $5,500

The following example will grep all the lines that contain both “Manager” and “Sales” in it (in any order).
$ grep -E 'Manager.*Sales|Sales.*Manager' employee.txt


15) Grep NOT

grep -v 'pattern1' filename

$ grep -v Sales employee.txt
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500

$ egrep 'Manager|Developer' employee.txt | grep -v Sales
200  Jason   Developer  Technology  $5,500
400  Nisha   Manager    Marketing   $9,500


16) Highlighting the search using GREP_OPTIONS

$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'

$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.



17) Show only the matched string

#] grep -o  

$ grep -o "is.*line" demo_file

is line is the 1st lower case line
is line
is is the last line

No comments:

Post a Comment