Grep command in Unix/Linux

How to use grep command in Unix/Linux.

Grep command is the most used command by the linux users. Full form of grep command Global search for Regular Expression and Print. Grep was originally developed for unix operating system but now it is available in both UNIX/LINUX operating system. Grep command is used to get the exact word or line from a file using the pattern . Even it is a tons of lines in a file using grep command it can separated easily. Here i am going to explain you grep command with few examples.

 

Grep multiple words
#grep ‘awk\|welcom’ blog
#grep -E ‘awk|welcom’ blog
Grep multiple words with egrep command
#egrepĀ  ‘awk|welcom’ blog
#grep -e’awk’ -e ‘welcom’ blog
Result comes with the line number where the matches are found
#cat new.txt | grep -n ‘linux’

 

Reverse search the pattern, It will search except linux
#cat new.txt | grep -v ‘linux’

 

It will find the number of matches and count the matches like [wc -l].
#cat new.txt | grep -c ‘linux’

 

It will grep only the exact word same like [/< />]
#cat new.txt | grep -w ‘linux’

 

Recrusively search through out the folders and files
#cat new.txt | grep -R ‘linux’
#cat new.txt | grep -r ‘linux’

 

Ignore k-sensitive while searching for a matches
#cat new.txt | grep -i ‘linux’
#cat new.txt | grep -y ‘linux’

 

Prints the exact word even if the match word contains some other letters
#cat new.txt | grep -o ‘linux’
Prints the matches and +(n) number of lines after the pattern matches
#cat new.txt | grep -A(n) ‘linux’

Prints the matches and +(n) number of lines before the pattern matches
#cat new.txt | grep -B(n) ‘linux’

To find the version of the grep command
#grep -V