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

Shell regular expression

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

Share

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

Wildcard meaning

* match any number of characters (including zero or one)

Huh? match any character (excluding zero)

[characters] matches any character that belongs to the character set

[! characters] matches any character that is not in the character set

[[: class:]] matches any character that belongs to the specified character class

Character class meaning

[: alnum:] matches any letter or number

[: alpha:] matches any letter

[: digit:] matches any number

[: lower:] matches any lowercase letter

[: upper] matches any uppercase letter

The basic regular expression symbol describes an example. Represents a single character (must exist)

A..b

Can represent acdb

Aaab 、 abbbb

But it cannot represent acb, ab, aba, etc.

*

To be distinguished from wildcards, match * 0 or more of the previous character

The character after * must be after the character before *.

In addition, the characters before and after * must be adjacent or they can't match.

Axib

Can represent ab, aab, b, abababab

But it cannot represent acb, ba, etc.

Acb only matches to b, that is, it is regarded as having 0 an in front of b.

Ba will only match to b, which is also regarded as having 0 an in front of b

^ matches the beginning of the string after ^

Enter: echo-e abcd\ accc | grep "^ ab"

Output: abcd

$matches the end of the string before $

Enter: echo-e abcd\ accc | grep "cc$"

Output: accc

. * match any character (0 to more)

A. Roomb

It is roughly similar to the * in wildcards.

Can represent ab, acb, a. B

But it doesn't represent b... A

[]

Represents the range, which can be used for fuzzy matching

The common option is [amerz] [0-9]

1. Grep [0-9] text filters out rows with numbers

2. Grep [a murz] text filters out lines with lowercase letters

3. Grep [abc] text filters out lines that contain an abc (note that this abc matches separately, that is, it matches a line, b line, c line, and not the string line).

{}

{nrecoery m} means to match at least n and up to m characters in front of {}. Note: they are consecutive characters connected together.

It can also be {n,} to indicate at least n

{, m} means a maximum of m

1. Enter echo abbcdfbjk | grep-E "b {1je 2}"

Output abbcdfbjk

two。 If you enter echo abbcdfbjk | grep-E "b {2,}" # indicates at least two

The output result is abbcdfbjk. You can see that only the two bb connected together are matched, which means that the numbers in {} do not represent the number of matches, but the length of matching characters. For example, the length of b is required to be at least 2, so only bb is matched.

3. Enter grep-E "ac {2j 5} b" to match at least two lines of up to five c between an and b, but the acb should be concatenated

[^] [^ a] means to match all characters except a

Enter echo-e abc\ def\ lmn | grep "[^ fmerz]"

Output abc def lmn, only [a mure] matches to

^ [^] matches all lines in ^ [^ a] except starting with a grep "^ [^ #]" / etc/passwd matches all lines in / etc/passwd that do not begin with #\

< ; \>

Anchor the beginning (tail) of the word

Represents a specified character that matches a specified character separated by a space or special character at the beginning (tail). If you anchor both the head and the tail\

< \>

Indicates the exact match of this character, which is separated by special characters or spaces, and will not be directly connected to any character.

Note:\ escape must be added, even if extended regular is used.

Enter: echo "hi,rootamroot" | grep "\"

Output: hiroot iamroot

Enter: echo "hi,root iamroot" | grep "\"

Output: hi,root iamroot

(.)

\ 1 call the previous parameter-the first grouping (that is, the parameter in parentheses)

If you use extended regular, you don't need\ to escape.

Filter out rows with two identical numbers in a row

# grep "\ ([0-9]\). *\ 1" / etc/passwd

Filter lines with the same first line letter and line position letter

# grep "^\ ([amurz]\). *\ 1 $" / etc/passwd

Extended regularity

The extended regular contains the basic regular, and there are more +? | | () four instructions (note here | to be separated from pipe characters) extension regular does not need to escape some symbols like basic regular (usually with backslash\) |

+

Function: repeat one or more of the previous characters

Example: execute the command "egrep-n 'wo+d' test.txt" to query strings such as "wood", "woood", "woooooood", etc.

?

Function: the first character of zero or one

Example: execute the command "egrep-n 'bes?t' test.txt" to query the two strings "bet" and "best"

| |

Function: use or (or) to find multiple characters

Example: execute the command "egrep-n'of | is | on' test.txt" to query the string "of" or "if" or "on"

()

Function: find the "group" string

Example: "egrep-n't (a | e) st' test.txt". "tast" and "test" because the words "t" and "st" repeat, "a" and "e" are listed in the "()" symbol and separated by "|" to query the "tast" or "test" string.

() +

Function: to identify multiple duplicate groups

Example: "egrep-n'A (xyz) + C 'test.txt". The command begins with "A" and ends with "C", with more than one "xyz" string in the middle.

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