Computer scienceSystem administration and DevOpsCommand lineText processing

Grep useful options

6 minutes read

Earlier we dealt with the grep command. In addition to basic options, with which you can, for example, find a word, filter search results, or ignore letter cases, there are more advanced options. With their help, you can display the context for the word, set up a recursive search, or search the match for a regular expression. For each of these tasks, there is a separate grep option, and below we will analyze them in more detail.

Displaying the context

It is sometimes useful to look at the context in which an entry occurs in a document. The context refers to the lines around the entry you are searching for. These can be lines before, after, or immediately before and after the entry. In order to display such lines, there are three options: -A <n>, -B <n>, and -C <n>, respectively. The n is the number of lines, and it is configurable. To sum up,

  • -A <n> shows the entry and n lines A(fter) it,
  • -B <n> shows the entry and n lines B(efore) it,
  • -C <n> shows n lines before and after the entry, i.e. C(ontext).

Let's consider the following example: there is a king.txt file with a poem and we are looking for the word "hat" in it. We are also interested in looking at the 3 lines before this word. That is, our context will be of size 3 and since we need lines before the word, we need option B:

$ grep -B3 "hat" king.txt

I often wish I were a King,
And then I could do anything.
If only I were King of Spain,
I'd take my hat off in the rain.

We may also want to find the word "rain" with 4 lines after it. For this, we will use the A option:

$ grep -A4 "rain" king.txt

I'd take my hat off in the rain.
If only I were King of France,
I wouldn't brush my hair for aunts.
I think, if I were King of Greece,
I'd push things off the mantelpiece.

And in case we need the word "France" with 1 line before and 1 line after, we will use the C option and the context will be of size 1:

$ grep -C1 "France" king.txt

I'd take my hat off in the rain.
If only I were King of France,
I wouldn't brush my hair for aunts.

Okay, now you know how to display the result with its context. Let's move to the recursive search.

If you need to search for text in multiple files located in the same directory or subdirectories, you should use a recursive search. To enable recursive searching, grep has the -r option. For example, you have a folder with different files and you want to find the word "rain" in them.

$ grep -r "rain"

railway.txt:Fly as thick as driving rain;
king.txt:I'd take my hat off in the rain.

Thus, the search will be conducted in all the files that are in the folder and also in all the subfolders that may be in it. In this example, there were just separate files, and the word "rain" was found in two of them.

In case you only need occurrences and it does not matter in which specific files they were found, add -h:

$ grep -rh "rain"

Fly as thick as driving rain;
I'd take my hat off in the rain.

Did you know that you can search not only for specific words, but for regular expressions? Below we will figure out how to do it.

Regular expressions

To use a regular expression pattern, you need to write it in quotes after the grep command and indicate the name of the file in which you will do the search: grep "regular expression" <filename>. For example, you have a list of names and want to find all the names that start with the letter "A":

$ grep "^A" names.txt

Anna Schwartz
Anna Lisa Brown

Also, you can combine the search by regular expressions with the recursion in case you have several files in the directory:

$ grep -rh "^A"

Alina Weis
Alisa Stone
Anna Schwartz
Anna Lisa Brown

Conclusion

To sum up,

  • you can use -A <n>, -B <n>, and -C <n> options to display the search result with the context
  • if you have several files or subdirectories you'd want to find entries in, you can use recursion -r
  • you can search by regular expressions and combine them with recursion if necessary.
86 learners liked this piece of theory. 2 didn't like it. What about you?
Report a typo