Grep command in Linux

In this tutorial, we are going to learn how to use grep command in Linux. We use grep command to search within files or output of text. Grep command can also be combined with other commands with the use of the pipe “|” operator. The grep command in Linux can be used to search a pattern or regex (Regex stands for Regular expression). There are many uses of grep command that we are going to discuss in this tutorial.

The basic syntax of grep is as follows:

$ grep 'word' filename
$ grep [option] 'word' filename
Grepthe 1

Consider the following two files grepdemo.txt and grepdemo1.txt as examples are performed based on this text.

Grepdemo1text
Grepdemotext 1

Proceeding further, here we will discuss some options that are used with grep command in Linux:

  • -o: shows only the matched string.
  • -b: prints the position of the matched string
  • -c: to count the repetitions of the matched string
  • -H: to print the filename with the matched string
  • -i: to print the matched string irrespective of that it is uppercase or lowercase.
  • -n: to print the line number of file with the line matched
  • -v: to print those lines which don’t include the matched string.

Search a Word in a File Using grep Command in Linux

As discussed earlier, the grep command is used to search a particular word from a file. To do so, we just need to mention the word and the file name.

The syntax for this is as follows:

$ grep "text to find" [filename]
Grepsearch

In the above screenshot, the grep command in Linux searches for the word “linux” in grepdemo.txt and will print the lines that contain the specified string. As shown above, it has appeared only one time in the file.

Search a word from all files

In the above example, we discussed how to search a word from a particular file. Here, we will discuss how to search a word in a group of files. For this, we will use the ‘*’ symbol. Let’s understand from an example

$ grep 'word' grep*.txt
Grepstar

Here, it will print “the” occurred in all the files where the filenames start with “grep” and have a “.txt” extension

Searching RegEx Patterns Using grep Command in Linux

You all know regex stands for regular expressions. Grep command can also search for a regex pattern. Let’s understand with an example.

Consider the syntax:

$ ls -l | grep ^d
Gepd

In this example, the grep command will search for filenames beginning with the letter “d”. Using the ls command will list all the files and folders located at the desktop. Here it has listed 8 files/folders where ‘d’ comes at the starting. You can also use this syntax to search for any regex.

Print Only the Requested String Using the grep Command in Linux

You might have noticed that by default grep command prints the line containing the searched keyword. If you want the grep command in Linux to display only the matched string of the pattern then use ‘-o’ option. Let’s understand this from an example.

The syntax for this is as follows:

$ grep -o "the" grepdemo.txt
Grep0

Here, it will print only the searched keyword instead of the complete line.

Show the position of the matched pattern

The “-b” option for the grep command in Linux lets you print the position of the matched string. Let’s understand from the following example.

Consider the syntax:

$ grep -o -b "is" grepdemo.txt
Grepb

Here, it will print the position at which the particular keyword has occurred on each line. I have combined ‘-b’ with ‘-o’ so that it could only print the searched string.

Color or Highlight the Searched String

If you want that searched string should be of a different color, you can use GREP_COLOR. For this first, export the file and then run grep command. Consider the following example.

$ export GREP_COLOR='100;10'
$ alias grep='grep --color=always' 
$ grep --color=always "the" grepdemo.txt
Grep Color Output
Grep Color Output

Here, in this example the searched keyboard is highlighted. In the second figure, it will only mark the positions where the search keyword is located. Remember, you have to use export command if you want to highlight the search keyword otherwise it won’t work.

Count the Repetitions Using the grep Command in Linux

We can also use the grep command in Linux to count the repetitions of the searched string. For this, we can use the ‘-c’ option. Consider the example:

$ grep -c "word" grep*.txt
Grepcount

The above screenshot demonstrates how many times the keyword “testing” is repeated in a particular file. Here, I have counted the searched keyword in all the files. You can also search it in a single file. Just remove ‘*’ and mention the file name.

Print The File Name With The Searched String

Earlier, we were just printing the lines or keywords of the searched string. But in this case, we will also print the file name including line too as shown in the given figure below. We will use the ‘-H’ option with grep command.

Consider the syntax of the following:

$ grep -H "Word" [filename]
Greph

In this example, it will print the file name as well as the line in which the searched string is present.

Case Insensitive Search Using The grep Command in Linux

This command option ignores the uppercase and lowercase condition and searches for the word in the file. Suppose, you want to search “linux”, it will search and print all the lines where this word has occurred irrespective of its case. It doesn’t matter that either the match string is in uppercase or lowercase. It will just print the line if the match found as shown in the example given below.

The syntax for the following is given below:

$ grep -i "linux" grep*.txt
Grepi

Store the grep Command Output in a File

What if you want to save the searched result. As discussed in earlier posts, ‘>’ is used for the output redirection. It will save the output of the command in a file as shown in the figure given below.

The syntax for the following is:

$ grep "word" [filename] > [filename]
Grepinput

In the example, the result will get stored in the file named text1.txt.

Display Line Numbers on grep Command Output

We know how to print the file name with the searched keyword. Now, what if you want to print the line number of file with the line matched. ‘-n’ option with grep command is used to print the line number of the file with the line matched as shown in the figure given below.

The syntax for the following is:

$ grep -n "word" [filename]
Grepn

Here, it will print the line number where the searched keyword has occurred.

Invert the Match Using grep Command in Linux

Till now we were searching for the keyword and printing that line. What if we want to print the line which doesn’t include matched string? ‘-v’ option is used for this as shown in the figure given below.

The syntax for the following is:

$ grep -v "word" [filename]
Grepv

Here, it will print those lines which don’t include searched keyword.

Check the Version

To check the version of grep command in Linux, we use the ‘–version’ option with it.

The syntax is:

$ grep --version
Grepversion

As shown in the figure, it will print the version of the grep command.

Combining Multiple Options

We can also combine multiple options in grep command in Linux as shown in the figure given below.

The syntax for the following is :

$ grep -v -n "word" [filename]
Grepvn

Here, we have combined ‘-v’ and ‘-n’ option. ‘-v’ is used as an inverted match while ‘-n’ is used to print the line number. Therefore, it will print those line numbers where this search keyword hasn’t occurred. You can see in the figure given above. It has printed those line number and lines which doesn’t include “linux” string.

Conclusion

In this tutorial, we’ve gone through how to use the grep command in Linux. We hope that you now know how to use the command efficiently. If you have any questions, do let us know in the comments.Â