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

How to use the Grep command in Linux

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

Share

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

This article mainly shows you "how to use Grep commands in Linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Grep commands in Linux" this article.

Command name: grep

The function of the command:

Text search or search tool

Details:

It can also be used with regular expressions to search for text and print out matching lines. It can also be used to filter and search specific strings, which is very flexible.

Common parameters:

-a # Don't ignore binary data

-A # displays not only the line that matches the template style, but also the content after that line

-b # in addition to the line that matches the template style, and displays the content before the line

-B # displays not only the line that matches the style, but also the content before the line

-c # calculates the number of columns that match the template style

-C # displays the contents of the column before and after the column in addition to the column that matches the template style

-d # this parameter must be used when you specify that you are looking for a directory instead of a file, otherwise the grep command will return information and stop the action

-e # specifies the string as the template style for finding the contents of the file

-E # uses the template style as an extended normal representation, which means that you can use extended regular expressions

-f # specifies a template file with one or more template styles and lets grep find the content of the file that meets the criteria of the template in the format of the template style for each column

-F # treats the template style as a list of fixed strings

-G # treats the template style as a common representation

-h # do not indicate the name of the file to which the column belongs until the column that matches the template style is displayed

-H # Mark the name of the file for that column before displaying the column that matches the template style

-I # ignores the difference in character case

-l # lists the file names whose contents match the specified template style

-L # lists the file names whose contents do not match the specified template style

-n # Mark the number of the column before displaying the column that matches the template style

-Q # does not display any information

The effect of this parameter is the same as that of specifying "- d recurse" parameter.

-s # does not display error message

-v # reverse lookup

-V # displays version information

-w # displays only columns that match the whole word

-x # displays only the columns that match the entire column

-y # this parameter has the same effect as "- I"

-o # output only those parts of the file that match

Regular expression

^ # match lines that start with XX

$# matches lines ending in XX

Common examples:

1. Look in multiple files:

Grep "file" file_1 file_2 file_3

2. Output all lines except-v option:

Grep-v "file" file_name

3. Tag matching color-- color=auto option:

Grep "file" file_name-color=auto

4. Use the regular expression-E option:

Grep-E "[1-9] +"

Egrep "[1-9] +"

5. Output only the-o option that matches in the file:

Echo this is a test line. | | grep-o-E "[amurz] +\."

Line.

Echo this is a test line. | | egrep-o "[amurz] +\."

Line.

6. Count the number of lines in the file or text that contain matching strings-c option:

Grep-c "text" file_name

two

7. Output the number of lines containing matching strings-n option:

Grep "text"-n file_name

Or

Cat file_name | grep "text"-n

8. Multiple files

Grep "text"-n file_1 file_2

9. Search multiple files and find out which files match the text:

Grep-l "text" file1 file2 file3...

10. Grep recursively searches for files

Recursively search for text in a multi-level directory:

Grep "text". -r-n

11. Ignore the character case in the matching style:

Echo "hello world" | grep-I "HELLO"

Hello

12. Option-e specifies multiple matching styles:

Echo this is a text line | grep-e "is"-e "line"-o

Is

Line

13. You can also use the * *-fallow * option to match multiple styles and write out the characters you need to match line by line in the style file.

Cat patfile

Aaa

Bbb

Echo aaa bbb ccc ddd eee | grep-f patfile-o

14. Include or exclude specified files from grep search results:

Only recursively search for the character "main ()" in all .php and .html files in the directory

Grep "main ()". -r-- include *. {php,html}

15. Exclude all README files from the search results

Grep "main ()". -r-- exclude "README"

16. Exclude files from the list of filelist files in the search results

Grep "main ()". -r-- exclude-from filelist

The above is all the contents of this article entitled "how to use Grep commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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