Learn about command grep

For Linux operating system users, the grep command is not too strange. With grep can help us search faster and more accurately, easy to customize the search option. Today I would like to share some options of grep


「grep」 is the command to display the line containing the string of characters in the file. You can specify multiple files or multiple paths of the search object. It is possible to replace the file or path with the output from another command. This is a common use case when used with grep.

2.Gep's options

*Option related to pattern search

*Option related to display

3. Only output lines containing search results

From the result of the command dmesge(command to display the message system at startup), in case we want to display the lines but only the video clusters, we use the command, we use the command 「dmesg | Grep video」

4. Search word by word

In the search section above, searching for

results in 「uvcvideo」but in case you only want to display each word

, we use the -w option, the search results will be like below.


Grep -w
Dmesg | Grep -w video
(show only the line containing the phrase 「video」 from the execution result of the dmesg command)

Grep -w video file_text
(only display the line containing the phrase 「video」 from the text file )

5. Display the line before and after the line containing the word you want to search

When searching for character strings, there are cases where we want to display the lines before or after the line containing the match results to make it easier to understand, then we use the option -number_line where number_line is the number of lines displayed before and after For example: in In case you want to display two lines before and after, specify by the number 2 as shown below.


Dmesg | Grep -w -2 videos
(display includes 2 lines before and after the line containing the character (-w option) video from the execution result of the dmesg command)
Grep -2 [search string] filetext
(seach character string from text file, show both previous result lines
You can specify the line behind or the line before the result line using the following 2 options

6.Search for multiple characters (using the -e option)

In case we want to search for many characters, we will add the option -e in the following format:
Grep -e text 1 -e text2 -e text3 file text
With the -e option, we can also add other options. For example, if you want to search for characters that are not case sensitive, we add the -i option according to the format below.
Grep -i -e text1 -e text2

7.Search multiple characters (using regular expressions)

There are two ways to search multiple characters. One is to use the -E option or attach a 「」 sign before 「|」. Either way, attach a quote to the text string you want to search for.


Grep ‘text1|text2|text3’ file text

Grep -E ‘text1|text2|text3’ file text

8.Search from a string of characters in any file

In case we want to search the list of characters in the file, we use the -f option with the following format


Grep -f list text file text
Command | Grep -f list search characters
For example, we create a wordlist file with the content of 2 keywords network and video, when searching the results will be as follows

9.Want to search line containing any character

We can specify the search order according to the order of the regular expression [text 1, text 2, text 3] as shown below. Specifically, we can put the ‘.*’ in the middle of the characters we want to search


「grep “text1.*text2″」