Q&A – 8 Practical Examples Of grep Command In Linux

By | 22/08/2013

The grep command searches for a keyword or a pattern in the file or file(s) supplied as an argument to this command. Though grep command can do most of the work but still there exist three of its variants — egrep, fgrep and rgrep — that have their own capabilities. This article — presented in form of a Question & Answer — discusses grep command through 8 practical examples.

 

Grep Command Examples

Q1. How do I find a word in a file using grep command?

Ans. It is very simple. Just pass the keyword and the filename (along with complete path if not in current directory) as arguments to grep command.

Here is an example that uses grep command to find the keyword int in the file frnd.cpp:

grep-1

So you can see that grep command displayed all the lines that contain int keyword in the file frnd.cpp.

Q2. How to restrict grep to find exact words only?

Ans. As you can see in the output in the previous example, grep displayed those lines also where int keyword was part of another word like printf. So, in order to restrict grep command to find exact words only, use -w option along with it.

Here is an example :

grep-2

So you can see that only lines containing exact keyword (int in this case) were displayed in the output.

Q3. How to enable grep command to display line numbers in output?

Ans. Knowledge of line numbers is of great help while working with files containing thousands of lines. It helps to go straight to these lines after finding keywords through grep command and hence saves a lot of time. Enable grep command to display line numbers by using -n option.

Here is an example :

grep-3

So you can see that line number of each resultant line id displayed in the output.

Q4. How to display inverted results with grep command?

Ans. There are times when it is required to display those lines that do not contain the searched keyword. For these times, use -v option along with grep command.

Here is an example :

grep-4

So you can see that all the lines of file that do not contain the keyword int were displayed in the output.

Q5. How to display total number of lines that contain searched keyword using grep command?

Ans. To display only the number of lines containing searched keyword. Just use -c option along with grep command.

Here is an example :

grep-5

So you can see that a total of 6 lines contain the word int in the file efence_test.c.

Q6. How to search the keyword in more than one files?

Ans. In case you want to search the keyword in more than one files, you can either specify all the names or can use wild-card characters.

Here is an example where multiple file names are supplied:

vim-6-1

Here is an example where wild-card * is used:

vim-6-2

So you can see this way a single keyword can be searched within multiple files.

Q7. How to search multiple words in a single file?

Ans. To search multiple words, try using a grep command variant egrep. Just specify multiple keywords separated through pipes and enclosed in single quotes.

Here is an example :

grep-7

So you can search multiple keywords in a go using egrep command.

Q8. How to use grep command to search a keyword in the files produced as results by find command?

Ans. It’s simple, just use xargs command in this case along with find and grep commands.

Here is an example :

grep-8

So you can see that the grep searched each file (produced as output by the find command) for the keyword sleep(1).

 

NOTE – Want to learn more Linux commands? Read our articles on bc command, wc command and uname command.

NOTE – Want to learn interesting Linux command line tips and tricks? Read our article on 20 Interesting Linux Command Line Tips & Tricks.

 

 

11 thoughts on “Q&A – 8 Practical Examples Of grep Command In Linux

  1. Aram Iskenderian

    Hello,

    You have a typing error in Q4.

    “For these times, use -i option along with grep command.”

    You need to change that to -v, which you accurately had in your example screenshot in the same section.

    Reply
  2. BamaRob

    Q8 – it appears in your example you search for the term ‘sleep(1)’, but in your note below, you say you searched for ‘int’. Good write up. Thanks.

    Reply
  3. Dan Saint-Andre

    In Q8, you piped ‘find’ into ‘xargs’. Is there some reason to do it that way instead of the following:

    prompt$ find … -print -exec grep ‘sleep(1)’ {} \;

    Linux commands offer dozens of ways to accomplish the same thing. Often there is zero difference in the results but the chosen implementation often has subtle technical differences.

    Reply
    1. Himanshu Post author

      Well, that’s the way I usually do it. Although, It would be benificial if you can describe the technical difference between the two and which one is better?

      Reply
      1. Geoff

        The advantage of using find … -print | xargs … over
        find … -exec grep ‘sleep(1)’ {} \;
        is that xargs will pass multiple files to grep instead of executing one grep for each file. However, you can make find do that by using a ‘+’ instead of ‘;’ to terminate the command:
        find … -exec grep ‘sleep(1)’ {} +
        Another difference is that xargs will misbehave if any filenames contain whitespace, quotes or backslash characters, whereas having find invoke grep directly avoids that problem.
        Finally a tip: with xargs and with the find that uses ‘+’ there is a chance that the last execution of grep will only be passed one filename, and thus will not put the filename and a colon on the front of matching lines from that file. You can ensure all greps have at least two filename arguments by adding /dev/null to the command:
        find … -exec grep ‘sleep(1)’ /dev/null {} +

        Reply
  4. Anon

    In Q7, you can use grep in this instance. Using your example, it would be `grep -E ‘int|char’` or `grep -E ‘int’ -E ‘char’`. It’s worth noting that `egrep` and `grep -E` turns on extended regular expressions. If you were to not want the added functionality, then `grep -e ‘int’ -e ‘char’` would be acceptable

    Reply
  5. Alexi

    Nice write-up. I also like the -A and -B to show a number of lines before and after the line that matched.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *