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 regular expressions in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to use regular expressions in Linux. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Composition

Ordinary characters: ordinary strings with no special meaning

Special characters: have a special meaning in regular expressions

Common meta characters in regular expressions [special characters]

2. Meta characters in both POSIX BRE and ERE

\: usually used to turn on or off the special meaning of subsequent characters, such as (...) [\ is an escape character, remove the special meaning of the symbol, (), {}, etc. Have special meaning in shell]

. And. The difference between:

[root@localhost] # cat-n test.txt

1 gd

2 god

three

4 good

5 goood

6 goad

seven

8 gboad

2.1 、. : matches any single character (except null, that is, cannot be empty)

[root@localhost ~] # grep-n "." Test.txt

1:gd

2:god

4:good

5:goood

6:goad

8:gboad

[root@localhost ~] # grep-n "go.d" test.txt

4:good

6:goad

2.2,: match the preceding characters any number of times, such as o, can be no o or one o, or multiple o

[root@localhost] # grep-n "*" test.txt

[root@localhost] # grep-n "o *" test.txt

1:gd

2:god

3:

4:good

5:goood

6:goad

7:

8:gboad

[root@localhost ~] # echo "gbad" > > test.txt

[root@localhost ~] # echo "pbad" > > test.txt

[root@localhost ~] # echo "kgbad" > > test.txt

[root@localhost ~] # echo "poad" > > test.txt

[root@localhost ~] # grep-n "go*" test.txt [o may not exist, o the g in front of it must match]

1:gd

2:god

4:good

5:goood

6:goad

8:gboad

9:gbad

11:kgbad

* 2.3,. : matches any character (matches all), and can be empty * *

[root@localhost ~] # grep-n ". *" test.txt

1:gd

2:god

3:

4:good

5:goood

6:goad

7:

8:gboad

9:gbad

10:pbad

11:kgbad

12:poad

[root@localhost ~] # grep-n "go.*" test.txt

2:god

4:good

5:goood

6:goad

[root@localhost ~] # grep-n "po.*" test.txt

12:poad

[root@localhost ~] # echo "pgoad" > > test.txt

[root@localhost ~] # grep-n "go.*" test.txt [any character exists after matching go, which can be empty]

2:god

4:good

5:goood

6:goad

13:pgoad

[root@localhost ~] #

[root@localhost] # grep-n "o.*" test.txt

2:god

4:good

5:goood

6:goad

8:gboad

12:poad

2.4, ^: matches the regular expression immediately following to. At the beginning

[root@localhost tmp] # grep "^ root" / etc/passwd

Root:x:0:0:root:/root:/bin/bash

[root@localhost tmp] #

2.5, $: matches the regular expression immediately preceding to. End

[root@localhost tmp] # grep "bash$" / etc/passwd | head-1

Root:x:0:0:root:/root:/bin/bash

[root@localhost tmp] #

^ $: it means a blank line.

"# | ^ $": matches comment lines and blank lines that begin with the # sign

2.6, []: match any character in brackets

(such as [sS], match s or match S), where a hyphen (-) can be used to specify the range of hyphens (such as [(0-9)], matching any of the 0-9 characters); [^ 0-9] if the ^ symbol appears in the first position of the square brackets, it matches any character that is not in the list.

[root@localhost tmp] # cat hosts

192.168.200.1

192.168.200.3

A.b.123.5

23.c.56.1

1456.1.2.4

12.4.5.6.8

[root@localhost tmp] # grep-E'([0-9] {1pr 3}\.) {3} [0-9] {1pr 3} 'hosts

192.168.200.1

192.168.200.3

1456.1.2.4

12.4.5.6.8

[root@localhost tmp] # grep-E'^ ([0-9] {1jue 3}\.) {3} [0-9] {1pm 3} $'hosts

192.168.200.1

192.168.200.3

[root@localhost tmp] #

2.7,?: matches zero or more of the preceding characters

[root@localhost] # grep-E "go?d" test.txt

Gd

God

[root@localhost ~] #

[root@localhost tmp] # cat test

Do

Does

Doxy

[root@localhost tmp] # grep-E "do (es)?" Test

Do

Does

Doxy

[root@localhost tmp] #

3. Characters unique to POSIX BRE (basic regular)

{https m}: an interval expression that matches the single character that precedes it and repeats it, followed by a single character, such as S0-1}, that is, repeat s 0-1 times. {n} means to match n times; {n ~ m} means to match n to m times, {n,} means to match at least n times, and {, m} to match at most m times. [\ escape character]

4. Characters unique to POSIX ERE (extended regular)

4.1.The function is the same as that of BRE.

[root@localhost tmp] # grep-E'^ ([0-9] {1jue 3}\.) {3} [0-9] {1pm 3} $'hosts

192.168.200.1

192.168.200.3

4.2, +: matches one or more of the previous regular expressions

[root@localhost ~] # egrep "go+d" test.txt

God

Good

Goood

[root@localhost ~] #

4.3, |: match multiple strings [or relationship]

[root@localhost ~] # grep-E "3306 | 1521" / etc/services

Mysql 3306/tcp # MySQL

Mysql 3306/udp # MySQL

Ncube-lm 1521/tcp # nCube License Manager

Ncube-lm 1521/udp # nCube License Manager

[root@localhost ~] #

4.4, (): group filtering, backward reference

Packet filtering

[root@localhost ~] # echo "glad" > > test.txt

[root@localhost ~] # egrep "(la | oo)" test.txt

Good

Goood

Glad

() backward reference; when the previous matching part uses parentheses, the contents of the first parenthesis can be output in the latter part with\ 1; and so on.

[root@localhost tmp] # ifconfig | sed-rn 's#.*addr: (. *) (B.*) $#\ 1roomgp'

192.168.4.27

5. Metacharacters of regular expressions

5.1,\ b: match a word boundary

[root@localhost tmp] # cat test

Do

Does

Doxy

Agdoeg

[root@localhost tmp] # grep "do\ b" test

Do

[root@localhost tmp] # grep "\ bdo" test

Do

Does

Doxy

[root@localhost tmp] # grep "\ bdoes" test

Does

[root@localhost tmp] # grep "\ bdo\ b" test

Do

[root@localhost tmp] #

5.2,\ B: matches non-word boundaries, as opposed to\ b

[root@localhost tmp] # grep "do\ B" test

Does

Doxy

Agdoeg

[root@localhost tmp] # grep "do\ b" test

Do

[root@localhost tmp] #

5.3,\ d: matches a numeric character, equivalent to [0-9]

5.4,\ D: matches a non-numeric character, equivalent to [^ 0-9]

5.5,\ w: match letters, numbers, underscores, equivalent to [A-Za-z0-9 _]

There are still many metacharacters, so we won't list them one by one here.

Case: boot streamlining

[root@localhost ~] # chkconfig-- list | egrep-v "crond | network | rsyslog | sshd | sysstat" | awk'{print "chkconfig", $1, "off"}'| bash

The above is how to use regular expressions in Linux. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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

Internet Technology

Wechat

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

12
Report