Americas

  • United States
sandra_henrystocker
Unix Dweeb

Taking advantage of the grep command’s many options

How-To
May 11, 20234 mins
Linux

The grep command offers interesting options to help you find what you want from text files.

The grep command makes it easy to find strings in text files on Linux systems, but that’s just a start. It can be used to search through these files for multiple strings or regular expressions at the same time. It can also ignore case when needed, and it can count the lines in the resulting output for you. This post shows how to use grep in all these ways.

Basic grep

The simplest grep command looks like the one shown below. This “find string in file” command will show all the lines in the file that contain the string, even when that string is only part of a longer one.

$ grep word story
The wording suggests there was more to the story than anyone wanted to admit.
The sword had been left behind the shed. It was several days before it was

Finding multiple strings

There are a number of ways to search for a group of strings in a single command. In the command below, the ‘|’ character serves as an “or” function. The command will display any lines in the file that contain the word “xray”, the word “tape” or both.

$ grep -E 'xray|tape' 4letters
tape
xray

The -E option allows “extended regular expressions” to provide this function. Another option is to list each string separately following the -e option.

$ grep -e xray -e tape 4letters
tape
xray

You can also use a command like the one below to find multiple strings. The “|” characters separate each word that you want to find.

$ grep 'xray|tape' 4letters
tape
xray

This form of grep command is not limited to two strings. Add as many strings as you need.

$ grep 'xray|tape|hope|boat' 4letters
boat
hope
tape
xray

You can also use regular expressions like ^xr in commands to look for strings that begin with particular letters.

$ grep '^xr|tape|hope|boat' ../4letters
boat
hope
tape
xray
xref

The same search can be performed using grep‘s -e option. In this case, each string is included following its own -e.

$ grep -e ^xr -e tape -e hope -e boat 4letters
boat
hope
tape
xray
xref

If you only want to find exact matches for your strings, use a command like the one below that will only display strings when they are included in the file as full words – not substrings. The command below fails to find the word “xray” because the “y” is omitted and the -w option is used.

$ grep -w 'xra|boat' 4letters
boat

In the examples below, the line containing the word “session” is only included when the full word is used in the command.

$ grep -w 'fly|session' recording_commands
When you first open a session on the command line, the oldest commands in
their history command numbers during a single login session.
want "on the fly". In other words, type "script" and each command that
$ grep -w 'fly|sessio' recording_commands
want "on the fly". In other words, type "script" and each command that

Using regular expressions

Note that the grep -e command will allow you to search for strings that begin with a hyphen. Without the -e, this wouldn’t work.

$ grep -e -xray myopts
-xray

The command below does not find the string.

$ grep -xray myopts

Ignoring case

To find strings in text files regardless of whether the letters are in uppercase or lowercase, use the grep command’s -i (ignore case) option. In the examples below, the word “it” is found 10 times in the first case and 11 times when the -i option is used.

$ grep 'it' recording_commands | wc -l
10
$ grep -i 'it' recording_commands | wc -l
11

Counting lines

And here’s a little surprise. Instead of using the wc command to count the lines in the grep output, you can use the grep command itself. Just add the -c (count) option. The commands below generate the same results as the previous two commands without piping the output to the wc command:

$ grep -c 'it' recording_commands
10
$ grep -ic 'it' recording_commands
11

Wrap-up

The grep command can do a lot more than find each instance of a single string in a text file. In fact, it can look for multiple strings in several different ways. It can also both ignore case and count the number of lines that match your request.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author