PoiNtEr->: How To Use Grep Command In Ubuntu(Linux)

                             Difference between a dream and an aim. A dream requires soundless sleep, whereas an aim requires sleepless efforts.

Search This Blog

Monday, March 26, 2012

How To Use Grep Command In Ubuntu(Linux)


Grep Command(linux)
                                                              


The grep command is one of the most useful in Linux. It may not seem that exciting at first, but once you learn about pipes you'll begin to see why it's so indispensable.Put simply, grep searches through one or more text files for a specific word or phrase. For any lines it finds which include the specified word or phrase, it will display them on the screen. It will also work with standard output , when combined with pipes.
Simple example: Your name is vishal and you know that somewhere in your /home directory is a file you've written for work. Let's say you know that it has the word vishalmishra in it. So you can instruct grep to search through all the files in your home directory like this:
grep -r vishalmishra /home/vishal/*


if you wanted to make it include results in both upper and lower case, then use the -i option:
grep -ir vishalmishra /home/vishal/*



Suppose you want to see n lines after the match has found then use -A:
grep -A 3 -ir vishalmishra /home/vishal/*

Now above command will show you next 3 lines after the match word.


Suppose you want to see n lines before the match has found then use -B:
grep -B 3 -ir vishalmishra /home/vishal/*

Now above command will show you  3 lines before the match word.


Use -C option to Display N lines around the match:
 grep -C 3 -ir vishalmishra /home/vishal/*


Use -v option for invert match:
grep -v -r vishalmishra /home/vishal/*


If You want to count the number of matches then use -c option:
grep -c -r vishalmishra /home/vishal/*


If you want to known only file names that contain a specific match then use -l:
grep -l -r vishalmishra /home/vishal/*




No comments:

Post a Comment