Linux grep command usage with examples

Posted on December 23rd, 2016

The grep command which stands for “global regular expression print,” processes text line by line and prints any lines which match a specified pattern. The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Grep is considered to be one of the most useful commands on Linux and Unix-like operating systems. grep is a powerful file pattern searcher in Linux. If it is not installed on your system, you can easily install it via your package manager (apt-get on Debian/Ubuntu and yum on RHEL/CentOS/Fedora). For installing grep in your system, please use the following command.

$ sudo apt-get install grep         #Debian/Ubuntu

$ sudo yum install grep             #RHEL/CentOS/Fedora

 

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines. In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F. rgrep is the same as grep -r. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified. The grep has no limits on input line length other than available memory, and it can match arbitrary characters within a line. If the final byte of an input file is not a newline, grep silently supplies one. Since newline is also a separator for the list of patterns, there is no way to match newline characters in a text.

 

Using grep for search files.

To search /etc/passwd file for the user harry, enter the following command.

$ grep harry /etc/passwd

Sample outputs:

harry:x:1000:1000:harry,,,:/home/harry:/bin/ksh

You can force grep to ignore word case i.e match harry, HARRY, Harry and all other combination with the -i option:

$ grep -i “harry” /etc/passwd

If you want to search for a word, and avoid matching substrings use ‘-w ‘option. Just doing a normal search will show all the lines. The following example is the regular grep where it is searching for “is”. When you search for “is”, without any option it will show out “is”, “his”, “this” and everything which has the substring “is”.

$ grep -i “is” samplefile

THIS LETTER IS THE 1ST UPPER CASE LETTER IN THIS LINE.

this letter is the 1st lower case letter in this LINE.

This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.

And this is the last line.

The following example is the WORD grep where it is searching only for the word “is”. Please note that this output does not contain the line “This Line Has All Its First Character Of The Word With Upper Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not for “this”.

grep -iw “is” samplefile

THIS LETTER IS THE 1ST UPPER CASE LINE IN THIS FILE.

this letter is the 1st lower case letter in this line.

Two lines above this line is empty.

And this is the last line

 

If you need any further assistance please contact our support department.

 

 

10 Responses to “Linux grep command usage with examples”

  1. abcd says:

    How to search for a sentence or couple of continues words in a directory?

    • Jithin says:

      grep ‘word1\|word2\|word3’ /path/to/file
      If you want to search the same in all files under a directory, then use the “-r” switch.

  2. Neil says:

    How do I find out lines that start with a digit.

  3. Davi says:

    I have a file which gives me the number of alphabets in each line. For example A=4 B=7 C=2,, etc. There’s a line that doesn’t have M in it, so it skips M in the counting process. So it reads …L=5 N=8….
    I’m trying to extract that line with the command: grep -w “L=* N=*” file.txt. But it’s not recognizing that line. How to get that working?

  4. Ernie Doronila says:

    hi newbie here, i’m curios, what grep -vE means

  5. Raquel says:

    How do I grep only directories beginning with ‘A’ or ‘a’

  6. Mily Mary says:

    hi,

    I was trying to grep all the success and failure status, from a log file to, the same log file at the end of execution. And this doesn’t seem to work. Could you please help me do it.
    the command I am using is:
    grep -i “0 – Successful” ${log} >> ${log}

    where ${log} is my log file

  7. Jeremias says:

    How do I grep a time in a different format than the original output from an sysctl -n kern.waketime output ?
    The Output is for example: { sec = 1583236988, usec = 11 } Tue Mar 3 13:03:08 2020
    While I need: 1303 in that example…

Leave a Reply