Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A powerful text search tool in linux-grep command

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Today, the editor shares with you the powerful text search tool in linux-grep command. I believe many people don't know much about it. In order to make you understand better, I summed up the following contents and looked down together. I'm sure you'll get something.

Grep is short for "global search regular expression and print out the line", which means to search for regular expressions across the board and print them out. This command can be used in conjunction with regular expressions, and it is also the most widely used command in linux.

The options of the grep command are used to complement the search process, and the mode of the command is very flexible, which can be variables, strings, or regular expressions. It is important to note that when a pattern contains spaces, be sure to enclose it in double quotes.

The linux system supports three forms of grep commands. The eldest son is the representative of grep, the standard and imitation. The second son has many hobbies-egrep, or extended grep command for short, which is actually equivalent to grep-E and supports basic and extended regular expressions. The youngest son runs the fastest-fgrep, or fast grep command for short, which is actually equivalent to grep-F. it does not support regular expressions and matches according to the surface meaning of the string.

Syntax format: grep [parameters]

Common parameters:

-I search, ignore case-c outputs only the number of matching lines-l lists only matching file names, not specific matching lines-n lists all matching lines Displays line number-h does not display file name when querying multiple files-s does not display error messages that do not exist and does not have matching text-v displays all lines that do not contain matching text-w matches whole words-x matches entire lines-r recursive search-Q forbids the output of any results, exit status indicates whether the search was successful-b prints the offset of matching lines from the header of the file, used in bytes in combination with-b Print matching words according to the offset of the file header, in bytes

Reference example

Support for multi-file queries and the use of wildcards:

[root@linuxcool ~] # 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

Output the number of matching character sequences:

[root@linuxcool ~] $grep-c zwx file_*

File_1:2

File_2:1

File_3:0

List all matching lines and display the line number:

[root@linuxcool] # 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

Displays all lines that do not contain a pattern:

[root@linuxcool ~] # grep-vc zwx file_*

File_1:7

File_2:4

File_3:5

File_4:2

The file name is no longer displayed:

[root@linuxcool] # grep-h zwx file_*

Zwx

Zwx

Zwxddkjflkdjfdlkfjlsdkj

Zwx

Dkfjlzwxejfkje

Zwx djfkdjf

Zwxedkfgj

List only the file names that match, not the lines that match:

[root@linuxcool ~] # grep-l zwx file_*

File_1

File_2

File_4

Text messages that do not exist or do not match are not displayed:

[root@linuxcool] # 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, searching not only the current directory, but also subdirectories:

[root@linuxcool] # 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"

To match the whole word, to explain it literally, is equivalent to an exact match:

[root@linuxcool ~] # grep zw* file_1

Zwx

Zwx

Zdkfjeld

Zw

Ze

Zwxddkjflkdjfdlkfjlsdkj

[root@linuxcool ~] # grep-w zw* file_1

Zw

Match the entire line, and print it out when the whole line in the file matches the pattern:

[root@linuxcool ~] # grep-x zwx file_*

File_1:zwx

File_1:zwx

File_2:zwx

No results are output, and the exited status indicates the result:

[root@linuxcool] # grep-Q zwx file_1

[root@linuxcool] # echo $?

0

[root@linuxcool] # grep-Q zwx file_5

[root@linuxcool] # echo $?

one

[root@linuxcool] # grep-Q zwx file5

Grep: file5: No such file or directory

[root@linuxcool] # echo $?

two

Find blank and non-blank lines in a file:

[root@linuxcool ~] # grep-c ^ $file_1

four

[root@linuxcool ~] # grep-c ^ [^ $] file_1

fifteen

Match any or repeating characters with "." Or "*" symbol to achieve:

[root@linuxcool ~] # grep ^ z.x file_1

Zwx

Zwx

Zwxddkjflkdjfdlkfjlsdkj

[root@linuxcool ~] # grep ^ z * file_6

Zwx

Dfkjd

Zzdfjkd

Zz dfdww

Haha on linux in the powerful text search tool-grep command to share here, I hope that the above content can have a certain reference value, you can learn to apply. If you like this article, you might as well share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report