grep command – powerful text search tool

grep is short for "global search regular expression and print out the line", which means to search regular expressions comprehensively and print them out. This command can be used in combination with regular expressions. It is also the most widely used command in linux.

The options of grep command are used to supplement the search process, and its command mode is very flexible, which can be variables, strings and regular expressions. Note: when a pattern contains spaces, be sure to enclose them in double quotation marks.

linux system supports three forms of grep commands. The eldest son is the representative of grep, standard and imitation. The second son is interested in love - egrep, which is referred to as the extended grep command for short. In fact, it is equivalent to grep -E and supports basic and extended regular expressions. The youngest son runs the fastest - fgrep, referred to as the fast grep command for short. In fact, it is equivalent to grep -F and does not support regular expressions. It matches according to the surface meaning of the string.

Syntax format: grep [parameter]

Common parameters:

-i

Ignore case when searching

-c

Output only the number of matching rows

-l

Only matching file names are listed, and no specific matching lines are listed

-n

List all matching lines and display the line number

-h

Do not display file names when querying multiple files

-s

Do not display error messages that do not exist and do not have matching text

-v

Displays all lines that do not contain matching text

-w

Match whole word

-x

Match entire row

-r

Recursively searching

-q

It is forbidden to output any results. The exited status indicates whether the search is successful

-b

Prints the offset, in bytes, of the matching line from the file header

-o

Used in combination with - b to print the offset of the header of the matched word data file, in bytes

Reference example

Support multi file query and wildcard:

[root@linux ~]# grep zwx file_* /etc/hosts
file_1:zwx
file_1:zwx
file_1:zwxddkjflkdjfdlkfjlsdkj
file_2:zwx
file_4:dkfjlzwxejfkje
file_4:zwx djfkdjf
file_4:zwxedkfgj

Number of output matching string lines:

[root@linux ~]$ grep -c zwx file_*
 file_1:2
 file_2:1
 file_3:0

List all matching lines and display the line number:

[root@linux ~]# grep -n zwx file_*
 file_1:1:zwx
 file_1:4:zwx
 file_1:10:zwxddkjflkdjfdlkfjlsdkj
 file_2:2:zwx
 file_4:3:dkfjlzwxejfkje
 file_4:4:zwx djfkdjf
 file_4:5:zwxedkfgj

Show all rows without schema:

[root@linux ~]# grep -vc zwx file_*
 file_1:7                                
 file_2:4
 file_3:5
 file_4:2

Do not show file names again:

[root@linux ~]# grep -h zwx file_*
 zwx
 zwx
 zwxddkjflkdjfdlkfjlsdkj
 zwx
 dkfjlzwxejfkje
 zwx djfkdjf
 zwxedkfgj

Only the matching file names are listed, and the specific matching lines are not listed:

[root@linux ~]# grep -l zwx file_*
 file_1
 file_2
 file_4

Do not display text information that does not exist or does not match:

[root@linux ~]# grep  -s zwx file1 file_1
 file_1:zwx       
 file_1:zwx
 file_1:zwxddkjflkdjfdlkfjlsdkj
 [root@linuxcool ~]# grep zwx file1 file_1
 grep: file1: No such file or directory  
 file_1:zwx
 file_1:zwx
 file_1:zwxddkjflkdjfdlkfjlsdkj

Recursive search, which searches not only the current directory but also subdirectories:

[root@linux ~]# grep -r zwx file_2 *
 file_2:zwx
 anaconda-ks.cfg:user --name=zwx --gecos="zwx"
 file_1:zwx
 file_1:zwx
 file_1:zwxddkjflkdjfdlkfjlsdkj
 file_2:zwx
 file_4:dkfjlzwxejfkje
 file_4:zwx djfkdjf
 file_4:zwxedkfgj
 initial-setup-ks.cfg:user --name=zwx --gecos="zwx"

Matching the whole word and explaining it literally is equivalent to exact matching:

[root@linux ~]# grep zw* file_1
 zwx                       
 zwx
 zdkfjeld
 zw
 ze
 zwxddkjflkdjfdlkfjlsdkj
 [root@linuxcool ~]# grep -w zw* file_1
 zw                         

Match the whole line. Only when the whole line in the file matches the pattern can it be printed:

[root@linux ~]# grep -x zwx file_*
 file_1:zwx
 file_1:zwx
 file_2:zwx

No results are output. The exited status indicates the results:

 [root@linux ~]# grep -q zwx file_1
 [root@linux ~]# echo $?
 0               
 [root@linux ~]# grep -q zwx file_5
 [root@linux ~]# echo $?
 1               
 [root@linux ~]# grep -q zwx file5
 grep: file5: No such file or directory
 [root@linux ~]# echo $?
 2              

Find empty and non empty lines in a file:

[root@linux ~]# grep -c ^$ file_1
 4              
 [root@linux ~]# grep -c ^[^$] file_1
 15               

Match any or repeated characters with "." or "*" symbols:

[root@linux ~]# grep ^z.x file_1
 zwx
 zwx
 zwxddkjflkdjfdlkfjlsdkj
[root@linux ~]# grep ^z* file_6
 zwx
 dfkjd
                      
 zzdfjkd
 zz dfdww
 haha

Posted by seco on Mon, 29 Nov 2021 04:37:56 -0800