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

What is the grep command of the Linux system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The content of this article mainly focuses on what the grep command of the Linux system is. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

The Linux grep command is used to find strings in a file that match the criteria; it can also be used to find files that contain the specified template style. It can use regular expression search to search for a specified string pattern in a file, list file names that contain substrings of matching patterns, and output lines of text that contain that string.

Grammatical format

Grep [parameters]

Common parameter:-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 the matching words according to the offset of the file header, and the reference instance supports multi-file query and the use of wildcards in bytes:

[root@linux265 ~] # 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@linux265 ~] $grep-c zwx file_* file_1:2 file_2:1 file_3:0 lists all matching lines and displays the line number:

[root@linux265 ~] # 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@linux265 ~] # grep-vc zwx file_* file_1:7 file_2:4 file_3:5 file_4:2 no longer displays the file name:

[root@linux265 ~] # grep-h zwx file_* zwx zwx zwxddkjflkdjfdlkfjlsdkj zwx dkfjlzwxejfkje zwx djfkdjf zwxedkfgj lists only the file names that match, but does not list the specific matching lines:

[root@linux265 ~] # grep-l zwx file_* file_1 file_2 file_4 does not display text messages that do not exist or do not match:

[root@linux265 ~] # grep-s zwx file1 file_1 file_1:zwx file_1:zwx file_1:zwxddkjflkdjfdlkfjlsdkj [root@linux265 ~] # 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@linux265] # 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" matches the whole word and literally explains it, which is equivalent to an exact match:

[root@linux265 ~] # grep zw* file_1 zwx zwx zdkfjeld zw ze zwxddkjflkdjfdlkfjlsdkj [root@linux265 ~] # grep-w zw* file_1 zw matches the entire line. When the entire line in the file matches the pattern, it is printed:

[root@linux265 ~] # grep-x zwx file_* file_1:zwx file_1:zwx file_2:zwx does not output any result, and the exited status indicates the result:

[root@linux265 ~] # grep-Q zwx file_1 [root@linux265 ~] # echo $? 0 [root@linux265 ~] # grep-Q zwx file_5 [root@linux265 ~] # echo $? 1 [root@linux265 ~] # grep-Q zwx file5 grep: file5: No such file or directory [root@linux265 ~] # echo $? 2 find blank lines and non-blank lines in a file:

[root@linux265 ~] # grep-c ^ $file_1 4 [root@linux265 ~] # grep-c ^ [^ $] file_1 15 matches any or duplicate characters with "." Or "*" symbol to achieve:

[root@linux265 ~] # grep ^ z.x file_1 zwx zwx zwxddkjflkdjfdlkfjlsdkj [root@linux265 ~] # grep ^ z* file_6 zwx dfkjd zzdfjkd zz dfdww thank you for reading. I believe you have some understanding of "what is the grep command of the Linux system?" go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!

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

Development

Wechat

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

12
Report