Filter output and files using the GREP command in Linux

Home > Search > Linux commands
  by

The grep (global regular expression print) command can be used to only display lines from a file that contain a certain string. For example, let's say that example.file contains the following text.

Hello world
How are you today?
I sure could use some $ money

 

Grep can be used to search example.file with a cetain string of data. In this example, only the lines that contain the string Hello are displayed.

~]# grep Hello example.file
Hello world

 

It is common to use pipe and grep together. 

~]# cat example.file | grep Hello
Hello world

 


Does not contain

The -v or --invert-match option can be used to search a file for lines that do not contain a certain string. In this example, lines that do not contain the word Hello are displayed.

~]# grep -v Hello example.file
How are you today?
I sure could use some $ money

 


Case Sensitive

By default, grep is case sensitive. In this example, no results are displayed because the word Hello in example.com, has a capital H.

~]# grep hello example.file

 

The -i or --ignore-case option can be used to ignore the case.

~]# grep -i hello example.file
Hello world

 


White spaces

If the string of data contains one or more white spaces, place double quotes around the string.

~]# grep "are you" example.file
How are you today?

 


Search for multiple strings of data

The -E or --extended-regexp option can be used to search for numerous strings of data. In this example, the file is searched for both the words Hello and money.

~]# grep -E "(Hello|money)" example.file
Hello world
I sure could use some $ money

 


Contains entire word

The -w or --word-regexp option can be used to only display lines that contain an entire word. In this example, Hello world is not displayed because the -w option was used, and only the first three characters of the word Hello are used in the search.

~]# grep -w Hel example.com

 

Using the entire word Hello in the search will return the line that contains the phrase Hello world.

~]# grep -w Hello example.com

 


Special characters

Grep will work properly with some special characters. In this example, only line that contain the ? character will be displayed.

~]# grep ? example.file
How are you today?

 

On the other hand, some special characters will cause problems. In this example, lines that do not contain the $ character are displayed. This happens because the $ character is a regular expression character used to display lines that end with a string.

~]# grep $ example.file
Hello world
How are you today?
I sure could use some $ money

 

For example, the $ character can be used to only display lines that end with the word world. Fgrep can be used to search a file literally, ignoring regular expressions

~]# grep world$ example.file
Hello world

 

As another example, let's say example.file contains a line with the text (Line 4) and (Line 5). The following command will produce an error.

~]# grep (Line 4) example.file
-bash: syntax error near unexpected token `('

 

There are a couple ways to resolve this. One ways is to place double-quotes around (parenthesis).

~]# grep "(Line 4)" example.file
(Line 4)

 

Another option is to escape the parenthesis by placing a backslash before the parenthesis.

~]# grep \(Line 4\) example.file
(Line 4)

 

In this scenario, the * (wildcard) character can be used to search for any characters inside of the parenthesis.

~]# grep "(*)" example.file
(Line 4)
(Line 5)

 


Wildcard search

The period character is a wildcard in grep. In this example, any line that contains the letter o, then any characters, and then ld will be displayed.

~]# grep o.ld example.file
Hello world
I sure could use some $ money

 


Or search

In this example, there are no results, because there are no lines that contain the string abc.

~]# grep abc example.file

 

Brackets can be used to execute an or search. In this example, any lines that contain the letter a, or the letter b, or the letter c are displayed.

~]# grep [abc] example.file
How are you today?
I sure could use some $ money

 

The brackets can also contain a range of characters. In this example, any lines that contain the letts g through m are displayed.

~]# grep [g-m] example.file
Hello world
I sure could use some $ money

 


Counting

The -c or --count option can be used to count the number of lines that match a certain string. In this example, there is 1 line that contain the word Hello.

~]# grep -c Hello example.file
1

 


Search every file for a string

One of the most powerful grep commands is to search every file for certain text. Sometimes, you way what to use the -w option to only display results that match the entire word. Other times, -w is a bad idea. It all comes down to what you are looking for.

~]# grep -Rn '/path/to/search' -ie 'searchString'

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments