grep (Global Regular Expression Print)
The grep
(Global Regular Expression Print) command is a frequently used command to search for a string of characters or a pattern (regular expression) in specified files and returns the lines with the string or pattern. Using the -r
(recursive) option, you can search a string or pattern in multiple files under a specified directory. There are many other options to conduct more tailored searches.
To demonstrate the grep
command, we use the following directories and files in this section.
Directory structure
.
└── test
├── sample_1.txt
└── test_sub
└── sample_2.txt
I like apples and he likes oranges
I like oranges and he likes grapes
I like grapes and he likes bananas
I like bananas and she likes apples
He likes apples and she likes oranges
He likes oranges and she likes grapes
He likes grapes and she likes bananas
He likes bananas and I like apples
She likes apples and I like oranges
She likes oranges and I like grapes
She likes grapes and I like bananas
She likes bananas and he likes apples
I like the APPLE
I like the ORANGE
I like the GRAPES
I like the BANANAS
He likes the APPLE
He likes the ORANGE
He likes the GRAPES
He likes the BANANAS
She likes the APPLE
She likes the ORANGE
She likes the GRAPES
She likes the BANANAS
Basic grep syntax
To run the grep
command, you need to specify a search phrase (or pattern) followed by a file path.
For example, to search "apple" in the sample_1.txt file, run the command below.
grep apple test/sample_1.txt
The command returns the lines that have the string “apple”.
I like apples and he likes oranges
I like bananas and she likes apples
He likes apples and she likes oranges
He likes bananas and I like apples
She likes apples and I like oranges
She likes bananas and he likes apples
Options
There are many useful options for the grep
command.
-r (recursive) option
With the -r
option, you can search a string or pattern in multiple files under a specified directory.
For example, to search "He" under the test directory, run the command below.
grep -r He test
The command returns multiple lines with the string "He" from multiple files. The file path is shown before each line like in the example below.
test/test_sub/sample_2.txt:He likes the APPLE
test/test_sub/sample_2.txt:He likes the ORANGE
test/test_sub/sample_2.txt:He likes the GRAPES
test/test_sub/sample_2.txt:He likes the BANANAS
test/sample_1.txt:He likes apples and she likes oranges
test/sample_1.txt:He likes oranges and she likes grapes
test/sample_1.txt:He likes grapes and she likes bananas
test/sample_1.txt:He likes bananas and I like apples
-i option
The grep command differentiates upper and lower case characters; however, with the -i
option, you can search a string regardless of upper or lower case.
For example, to search "apple" regardless of upper or lower case, run the command below.
grep -ir apple test
The command returns the lines with the string "apple" and "APPLE".
test/test_sub/sample_2.txt:I like the APPLE
test/test_sub/sample_2.txt:He likes the APPLE
test/test_sub/sample_2.txt:She likes the APPLE
test/sample_1.txt:I like apples and he likes oranges
test/sample_1.txt:I like bananas and she likes apples
test/sample_1.txt:He likes apples and she likes oranges
test/sample_1.txt:He likes bananas and I like apples
test/sample_1.txt:She likes apples and I like oranges
test/sample_1.txt:She likes bananas and he likes apples
-n option
When you search data in a file with many lines of code, you may want to know the line number of the identified data. The -n
option gives you the line number along with search results.
For example, to search "apple" in the sample_1.txt file, with its line number, run the command below.
grep -n apple test/sample_1.txt
The command returns the lines with the string "apple" along with line numbers.
1:I like apples and he likes oranges
4:I like bananas and she likes apples
5:He likes apples and she likes oranges
8:He likes bananas and I like apples
9:She likes apples and I like oranges
12:She likes bananas and he likes apples
-C option
To understand the contexts of the searched lines of code, you may want to know what the lines of code before and after the identified lines of code are. The -C
option enables showing additional lines before and after the identified lines.
For example, to search "APPLE" in the sample_2.txt file, with one line before and after the identified lines with line numbers, run the command below.
grep -nC 1 APPLE test/test_sub/sample_2.txt
The command returns lines with the string "apple" and one line before and after the identified lines along with their line numbers.
1:I like the APPLE
2-I like the ORANGE
--
4-I like the BANANAS
5:He likes the APPLE
6-He likes the ORANGE
--
8-He likes the BANANAS
9:She likes the APPLE
10-She likes the ORANGE
Instead of using the -C
option, you can simply use the number of lines as an option. The command below gives you the same results as the previous command.
grep -n1 APPLE test/test_sub/sample_2.txt
-v option
The v
option is used when you want to show the lines that don't match the specified phrase or pattern.
For example, if you want to identify lines without the character "g", run the command below.
grep -v g test/sample_1.txt
The command gives you the lines like the ones below.
I like bananas and she likes apples
He likes bananas and I like apples
She likes bananas and he likes apples
-x option
The -x
option is used when you want to search the line that is exactly the same as the string specified by you.
For example, if you want to show the lines that are equal to "He likes bananas and I like apples", run the command below.
grep -x "He likes bananas and I like apples" test/sample_1.txt
The command gives you the line that is exactly the same as the string you specified:
He likes bananas and I like apples
Note : --color=auto option
The examples in this section show colored output for the grep
command. This is because of the --color=auto
option. As the OS that we are using for this course (Ubuntu OS on AWS Lightsail) has a preset alias for the grep
command with the --color option=auto
option, so the outputs of the command are already colored.
alias
:
alias grep='grep --color=auto'
:
For the alias
command, refer to Chapter 6 Alias (Create Command Shortcuts).