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

The usage of Linux regular grep/egrep

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

Share

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

* the main function of grep is to retrieve content based on keywords. Egrep is an extension of grep. Egrep includes all the functions of grep.

Grep usage: grep 'keyword' search object

Common parameters:

-c retrieve the number of lines containing keywords

-I is case-insensitive (keyword)

-n display line number

-v inverted (displays lines that do not contain keywords)

-r traverse all subdirectories (used to retrieve directories)

-An retrieves the lines containing keywords and the following n lines

-Bn retrieves the lines containing keywords and the n lines above

-Cn retrieves lines containing keywords as well as upper and lower n lines

-E equals egrep

Examples of special usage:

[root@localhost ~] # grep'[0-9]'/ etc/passwd

* retrieve rows containing numbers

[root@localhost ~] # grep-v'[0-9]'/ etc/passwd

* retrieve rows that do not contain numbers

[root@localhost ~] # grep'^ [0-9]'/ etc/passwd

* retrieve lines that start with a number

[root@localhost ~] # grep'[^ 0-9]'/ etc/passwd

* retrieve rows that do not contain numbers

[root@localhost ~] # grep'^ [^ 0-9]'/ etc/passwd

* retrieve lines that do not start with a number

[root@localhost ~] # grep 'r.t' / etc/passwd

*。 Means to match any character (number, letter, symbol, space)

[root@localhost ~] # grep 'oaks' / etc/passwd

* means to match 0 or more o (the character before the * sign)

[root@localhost ~] # grep'. *'/ etc/passwd

* means to match all characters (. Represents any character, and * represents 0 or more preceding characters)

[root@localhost ~] # grep'o\ {2\}'/ etc/passwd

* o {2} indicates that the row that appears twice in search o is equal to grep 'oo' / etc/passwd,\ escape character

[root@localhost ~] # egrep'o {2}'/ etc/passwd

* same as above, you don't need to add an escape character to use egrep.

[root@localhost ~] # egrep 'oaks' / etc/passwd

The * + sign indicates that it matches one or more characters preceding the + sign (o, oo, oo... Oo)

[root@localhost ~] # egrep 'oaks' / etc/passwd

*? Represents the character before 0 or 1 question mark

[root@localhost ~] # egrep 'root | test' / etc/passwd

* conduit character: user retrieves multiple keywords

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