Linux grep Command

The Linux grep command is used to extract lines of data from files or extract data from other Linux commands by using a pipe. When using the Linux grep command, there are many ways you can extract or print lines. You can use standard text to grep or you can use regex patterns. When using regex patterns you can use basic regular expression (BRE), extended regular expression (ERE), or even a Perl regular expression!

There are many different grep options you can use in the grep syntax. I myself only use a few options with grep on a regular basis. The basic grep syntax is grep [options] [pattern] [file|files].

grep quick reference guide:

You can use grep -R, grep -r, grep --recursive, which will allow you to have grep parse files recursivley into other directories.

If you are working with code, or just want the output lines to be numbered you can use grep -n or grep --line-number.

Another option I use on a daily basis with grep is grep -i or grep --ignore-case, which will ignore case on both the input file, and the pattern. by default grep is case sensitive, so you have to use this option if you do not want it case sensitive.

If you are wanting to extract multiple patterns from your file, you can use grep -e which can be used multiple times to extract multiple patterns from your file. So if you are wanting to grep two different strings from one file in Linux, you could do grep -e firstpattern -e secondpattern /home/$USER/file.txt.

Here are some grep examples of using the Linux grep command:

If I had a plain text file called phone-book.txt, that had phone numbers listed in this order: First Name Last Name - Street Address - Phone Number
Now I want to get Brandon Stimmel’s phone number. I could run grep Brandon\ Stimmel phone-book.txt and grep would print Brandon Stimmel - 100101 Digital Ave. Tech, Ohio 44333 (330) 222-7222. Notice I used Brandon\ Stimmel, I did this because you can not use a space on the command line, or it will be parsed as the next section of the command, which grep Brandon Stimmel phone-book.txt without the \ would try to extract Brandon from the file Stimmel, which doesn’t exist. So remember to escape your spaces with a \ if you are using them in your pattern/search string.

For another example, if I want to bring up who owns the phone number (330) 222-7222 as it showed up on my caller ID, but I forgot who’s phone number it was, I can do grep \(330\)\ 222-7222 phone-book.txt which would again display: Brandon Stimmel - 100101 Digital Ave. Tech, Ohio 44333 (330) 222-7222.

You can also grep for just the last name, say if you are having a family reunion and you want to bring up all of the people with the last name of Stimmel. grep Stimmel phone-book.txt this will bring up every person in phone-book.txt that has the last name of Stimmel.

You can also pipe data to grep or pipe data from grep to use it in a bash script. Say if you are wanting to do the above example that you found a phone number and forgot who’s it was, but you don’t want to show the entire line with name, address, and number, you just want the name and only the name. grep \(330\)\ 222-7222 phone-book.txt|awk '{print $1" "$2}' which will output Brandon Stimmel. The grep part of this code will output the full string, then we pass that data onto awk which we used it to print only the first and second fields.

Well that’s it for the grep tutorial, for now anyways. As always, thank you for reading our Linux Tutorials and I hope you find these Linux Tutorials very helpful! Remember to bookmark Beginner Linux Tutorial so you can always come back and find more helpful Linux Tutorials!

Comment Linux tutorial or Leave a Trackback

4 Comments

  1. maribel corcho
    Posted June 1, 2011 at 11:32 am | Linux tutorial comment

    Thank you for doing this. I am taking a linux course and the text book (by Jason Eckert) we are using is not the greatest. These tutorials really helps. Keep up the good work.

  2. sagar
    Posted June 23, 2011 at 7:12 am | Linux tutorial comment

    I really found it very useful. Thank you. I just stepped into Linux platform. Hope this tutorial will make me an expert in it.

  3. Posted June 23, 2011 at 8:23 pm | Linux tutorial comment

    Sagar,
    Thanks, glad you made the move to Linux. If you need any help or want to request a tutorial, please let me know.

  4. Manav Dayal
    Posted March 7, 2012 at 3:26 pm | Linux tutorial comment

    Found the tutorials really useful.
    Cheers for the effort.

3 Trackbacks

  1. […] grep – command used to search|find contents of a file and print|view on your terminal|screen. […]

  2. By Linux cat Command | Beginner Linux Tutorial on August 28, 2009 at 12:32 am

    […] Online Linux Tutorials for Beginners « How to Delete in Linux Terminal rm Command Linux grep Command […]

  3. […] are produced from another command or script. Here is an example of how to remove a file found after grep in Linux. Say if you have a folder /home/max/images/family and in this directory you have 20 images […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*