Grep Command with Regular Expression

Grep is used to search in content as per our requirement using different options, using this command we can save our time why late lets know about Grep command.

Grep stands for Global Regular Expression Parser,



How to check grep command available on our machine ?

  grep -V

How to install grep if not available in out machine ?

   sudo yum install grep

Syntax for Grep command
grep < > filename
cat filename | grep <string>

i have raja.txt with below content
ABC
abc
bcd
cde
123
234
....etc


1.How to search matched string in using grep command

cat raja.txt | grep cde
output
cde


2.How to search string without matched files

cat raja.txt | grep -v cde
ouput
ABC
abc
bcd
123
234


3.How to search string with case sensitive

cat raja.txt | grep -i abc
output
ABC
abc


4.How to print string with below n lines

cat raja.txt | grep  abc -A 2
output
abc
bcd
123


4.How to print string with above n lines

cat raja.txt | grep  123-B 2
output
abc
bcd
123


5.How to print below n line above n line with string

cat raja.txt | grep  abc -C 1
output
ABC
abc
bcd


6.How find count of matching strings

cat raja.txt | grep -c "abc"


7.Not Matched

cat a2z.txt | grep -v "Wood"


8.Match all lines that do not contain particular string

cat raja.txt | grep | “[^123]”


9.Match all lines that end with ‘done’. E.g: “well done”

cat raja.txt | grep | “done$”


10.matched content with number

cat a2z.txt | grep -n "Wood"


11.How find all .PDF extension files

grep -l *.pdf




Comments

Popular posts from this blog

Awk command with simple examples

Learn Linux in Telugu | Linux complete Free Course in Telugu by 7Hills

rsync Command Examples | rsync Command In Telugu