Home Command line tools 8 grep command examples you must know

8 grep command examples you must know

by The Linux Digest Guy

If I had to rank Linux commands by how often I use them. I think grep would easily rank in the top 5-10.

Grep simply takes a file or files and searches for a given pattern. It then prints out the line the pattern was found on.

Grep can come in handy in so many situations. Need to find in which configuration file a certain parameter is? Need to find a particular IP address in your apache logs? Grep can solve those problems easily.

I hope you find these grep examples useful.

Test file for our grep example commands.

Before we start we need a text file to search. I have created one that you can see in the attached image.

We will use this file to text our grep command examples.

Simple grep example

The simplest grep example is just to search for a string of characters in a file: grep <pattern> <file>

grep This test-file.txt
grep This test-file.txt

Grep with regex

Finding complex patterns of text can be done using regular expressions. Let’s try finding any text that is between quote mark. If you are not familiar with regular expressions, there are tons of resources that can be found on Google. I like Regex Tutorials.

grep -E '".*"' test-file.txt
grep -E '".*"' test-file.txt

You can also shorten this command by using egrep. Egrep is basically the same as writing grep -E.

egrep '".*"' test-file.txt

Grep all files in a directory

Looking through all files in a directory is simply a matter of replacing the filename with an asterisk (*). To make grep print the filename before each match use the -H option. If there are more than one files you can skip the -H option.

grep -H This my-directory/*
grep -H This my-directory/*

If you want to search all directories and subdirectories, you can use the -r option. This will instruct grep to recursively go through all directories and files in the path specified.

grep -r This my-directory/*

Grep in reverse

Want to have grep do the exact opposite of what it is supposed to do and print all the lines that don’t match and not the ones that match? No problem. Just use the -v option for invert match.

grep -v This test-file.txt
grep -v This test-file.txt

Print only the number of lines that match

If you only want to know how many lines contain your string you can use the -c option. Note that the -c option only counts the number of lines that match. Not how many times the pattern matches.

grep -c grep test-file.txt

Case insensitive grep

Normally grep is case sensitive. Some times you want to grep for a pattern where it doesn’t matter if the characters are upper case or lower case. In that case you can use the -i option.

grep -i ken test-file.txt
grep -i ken test-file.txt

Grep for multiple strings

If you have more than one pattern you need to grep for, -e specifies what pattern you want to use. You can use -e multiple times to use more than one pattern.

grep -e Ken -e file test-file.txt
grep -e Ken -e file test-file.txt

Grep lines before and after the match

This can be useful in cases where you are searching for a pattern in a log file but you want to know what came before or after the line you are matching. The -A, -B and -C options are what you need to know.

-A 2Print the next two lines after the matched line.
-B 2Print the last two lines that came before the matched line.
-C 2Print the last two lines that came before the matched line and also the next two after. Same as -A 2 -B 2.

As an example, you can try printing one line before and one line after the match with -C 1.

grep -C 1 Wikipedia test-file.txt
grep -C 1 Wikipedia test-file.txt

Using grep in bash scripts

Usually, the grep exit status is 0 if lines are found and 1 if none are found. If an error occurs, the exit status will be 2. If you don’t want the matched lines to be printed you would use grep’s -q option to suppress output.

Here is a sample bash script that checks if a pattern exists.

if grep -q Globally test-file.txt
then
  echo "The string was found"
else
  echo "The string was not found"
fi

Similarly, if you want to catch every possible exit status you could write a script like this.

grep -q Globally test-file.txt
grepresult=$?
echo $grepresult
if [[ $grepresult -eq 0 ]]
then
  echo "The string was found"
elif [[ $grepresult -eq 1 ]]
then
  echo "The string was not found"
elif [[ $grepresult -eq 2 ]]
then
  echo "An error occured"
else
  echo "Unknown exit code from grep"
fi

Conclusion

Hopefully, you have found these grep examples helpful. In the Linux command line, the grep command is used quite frequently. So you learning these and other grep options can be of great help.

2

Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy