Bootstrap FreeKB - Linux Commands - Filter output using grep
Linux Commands - Filter output using grep

Updated:   |  Linux Commands articles

The grep (global regular expression print) command can be used to filter output that matches or does not match a pattern. For example, let's say example.txt contains the following.

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

 

grep can be used to filter the output so that only lines matching Hello are returned.

~]# grep Hello example.txt
Hello world

 

Or like this.

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

 


The -v or --invert-match option can be used to filter the output that does not match a pattern. In this example, lines that do not contain Hello will be returned.

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

 

The ps command with the -ef flag should include a line line this.

~]$ ps -ef
john.doe   15809   9205  0 21:31 pts/2    00:00:00 grep --color=auto grep

 

The -v flag can be used to exclude the grep command from the output. It's also a good idea to wrap grep in quotes with whitespace to prevent matching ps command output that contains grep, just in case the ps command output contains grep, perhaps something like regrepository.

~]$ ps -ef | grep -v " grep "

 


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

~]# grep hello example.txt

 

The -i or --ignore-case flag can be used to ignore the case, and it's also a good idea to wrap grep 

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

 


If the string of data contains one or more white spaces, use quotes.

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

 


The -E or --extended-regexp option can be used to search for multiple strings of data. In this example, example.txt is searched for both the words Hello and money. This executes an "or" search, not an "and" search. In this example, any line in example.txt that contains Hello or money will be displayed.

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

 

Or, the egrep command could be used.

~]# egrep "Hello|money" example.txt
Hello world
I sure could use some $ money

 


The -w or --word-regexp option can be used to only display lines that contain an exact match. 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.txt

 

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

~]# grep -w Hello example.txt

 


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

~]# grep -c Hello example.txt
1

 


The grep command with the -R or --dereference-recursive flag and -e or --regexp option can be used to search every file below a certain directory for a pattern match. In this example, every file below the /opt directory will be searched for the text 'foo'.

grep -R --regexp 'foo' /opt

 

Often, the -i or --ignore-case flag is used to make the search case insensitive.

grep -R --ignore-case --regexp 'foo' /opt

 

A pipe can be used to exclude directories from the results, such as logs directories.

grep -R --ignore-case --regexp 'foo' /opt | grep -v logs

 

The -A option can be used to include lines following a pattern match. For example, let's say example.txt contains the following lines.

line one
line two
line three
line four
line five

 

In this example, the 2 lines after "line one" are included in the results.

~]# grep -A2 "line one" example.txt
line one
line two
line three

 

The -B option can be used to include lines before a pattern match. For example, let's say example.txt contains the following lines. In this example, the 2 lines before "line four" are included in the results.

~]# grep -A2 "line four" example.txt
line two
line three
line four



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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