Get the App
SLTechnology News&Howtos  ›  Servers  › 

The usage of Linux regular grep/egrep

Shulou Source: shulou.com Published: 2022-06-03 02:51:08 09月21日 Update

* 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

Tags: Search key keyword character number multiple semantic character representative keyword beginning special upper and lower no function content function parameter size subcategory subdirectory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS Shulou Technology Apple Linux