Grep is a useful command to search for matching patterns in a file. grep is short for "global regular expression print".

If you are a system admin who needs to scrape through log files or a developer trying to find certain occurrences in the code file, then grep is a powerful command to use.

In this article, we will discuss the grep command's syntax and its usage with some examples.

Syntax of the grep Command

The syntax of the grep command is as follows:

grep [OPTION...] PATTERNS [FILE...]
Grep syntax

In the above syntax, grep searches for PATTERNS in each FILE. Grep finds each line that matched the provided PATTERN. It is a good practice to close the PATTERN in quotes when grep is used in a shell command.

In this article, we will discuss the following options that can be used with grep:

  • -i, --ignore-case: Ignores case distinctions in patterns and input data.
  • -v, --invert-match: Selects the non-matching lines of the provided input pattern.
  • -n, --line-number: Prefix each line of the matching output with the line number in the input file.
  • -w: Find the exact matching word from the input file or string.
  • -c: Count the number of occurrences of the provided pattern.

In the coming examples, we will use the file fruits.txt with the following content:

apples and pears
citrus – oranges, grapefruits, mandarins and limes
stone fruit – nectarines, apricots, peaches and plums
tropical and exotic – bananas and mangoes
berries – strawBERRIES, raspberries, blueberries, kiwifruit and passionfruit
melons – watermelons, rockmelons and honeydew melons
tomatoes and avocados.
Contents of fruits.txt

How to Find a Matching String with grep

If we want to find the string "fruit" in the file fruits.txt, we can do so like this:

grep "fruit" fruits.txt

Here's the output:

image-73

How to Ignore Case Distinctions using -i

We can command grep to return results while ignoring the case of the matching string. Let's find the word "berries" from our sample file. It should match all occurrences of "berries" regardless of their case.

 grep -i "berries" fruits.txt

Output:

image-74

How to Select the Non-Matching Lines using -v

We can reverse the results of the grep command to include non-matching results. Let's say, if we want to get all the lines that do not contain the word "berries", the command would look like this:

grep -v "berries" fruits.txt
image-75

The output returned all the lines that do not contain "berries".

How to Find the Line Numbers Against Matching Input using -n

There are times when we want to get the line numbers against the matching string. For that, we can supply the -n flag to grep like this:

 grep -n "berries" fruits.txt

Output:

image-76

From the output, we can see that the word "berries" occurs on line #5 of the file.

How to Find the Exact Matching Word from the Input File or String using -w

If you were to match an exact word rather than just the occurrence of a pattern, you can do so by using the -w flag.

If we grep "fruit" without any flags, it would return any occurrence of the word "fruit".  This would include occurrences like "grapefruit", "dragonfruit" and so on like this:

image-77

But, if we only want "fruit" in our results, we can use the -w flag like this:

 grep -w  "fruit" fruits.txt
image-78

Do you see the difference? The latter result returned only one line where the exact word "fruit" was found.

How to Count the Number of Occurrences of the Provided Pattern using -c

We can count the number of times the matched string appears in the file. This is how the -c flag works:

grep -c "fruit" fruits.txt

Output:

image-79

The word "fruit" appears 3 times in the file.

Tip: The -c flag can be very useful if you have to count the number of times an error message appeared in a log file.

How to Scan Files for Matching Input

Until now we had discussed how to search for matching patterns in a file or input string. We can also use grep to narrow down the files that contain a matching pattern. We can use the following flags to separate files that contain the matching pattern:

  • -l, --files-with-matches: Prints the file name that contains the provided matching pattern.
  • -L, --files-without-match: Prints the file name that does not contain the provided matching pattern.

Let's say we have the following files in our folder:

zaira@Zaira:~/grep-tutorial$ ls -lrt
total 12
-rw-r--r-- 1 zaira zaira 327 Jan 16 19:25 fruits.txt
-rw-r--r-- 1 zaira zaira  47 Jan 16 20:31 vegetables.txt
-rw-r--r-- 1 zaira zaira 194 Jan 16 20:33 more-fruits.txt

The files have the following content:

zaira@Zaira:~/grep-tutorial$ cat fruits.txt
apples and pears
citrus – oranges, grapefruits, mandarins and limes
stone fruit – nectarines, apricots, peaches and plums
tropical and exotic – bananas and mangoes
berries – strawBERRIES, raspberries, blueberries, kiwifruit and passionfruit
melons – watermelons, rockmelons and honeydew melons
tomatoes and avocados.
Contents of fruits.txt
zaira@Zaira:~/grep-tutorial$ cat more-fruits.txt
passionfruit
dragon fruit
kiwis
pears
grapefruits, mandarins and limes
stone fruit – nectarines, apricots, peaches and plums
tropical and exotic – bananas and mangoes
tomatoes and avocados.
Contents of more-fruits.txt
zaira@Zaira:~/grep-tutorial$ cat vegetables.txt
# Vegetables only

Cabbage
Onion
Carrot
Potato
Contents of vegetables.txt

Next, we want to see files that contain the pattern "fruit". We can do that like this:

 grep -l "fruit" *

Note that the * searches for all the files in the folder.

Here is the output::

zaira@Zaira:~/grep-tutorial$  grep -l "fruit" *
fruits.txt
more-fruits.txt
fruits.txt and more-fruits.txt contain the string fruit

Let's say we want to list files that do not contain the word "fruit". We can achieve that using the command below:

 grep -L "fruit" *

Here is the output:

zaira@Zaira:~/grep-tutorial$  grep -L "fruit" *
vegetables.txt

Wrapping Up

If you want to study the grep command in depth, have a look at the man pages. You can get additional information on the command line with grep --help.

I hope you found this tutorial helpful!

What’s your favorite thing you learned from this tutorial? Let me know on Twitter!

You can read my other posts here.

Image credits:

Image by upklyak on Freepik