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 treat the regular expression of Linux system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces you how to look at Linux system regular expressions, the content is very detailed, interested friends can refer to reference, I hope to help you.

Introduction to Linux regular expressions

Learning Linux system friends will certainly come into contact with regular expressions, or developers will encounter this stuff, speaking of regular expressions, reminds me of "those who will not be difficult, difficult will not" this sentence, indeed, I believe many people just contact regular expressions, certainly confused, think it is difficult to learn, yes, migrant workers brother is such a feeling, but I believe everyone seriously to learn, to practice, certainly can learn.

So let's talk about regular expressions today

What is a regular expression? First of all, it is a text pattern, and then its role is to deal with a method of string.

Given a regular expression and another string, we can achieve the following

1. Whether a given string matches the filtering logic of a regular expression (called a "match")

2. Regular expressions can be used to get the specific part we want from a string.

The characteristics of regular expressions are:

1. Very flexible, logical and functional

2. Complex control of strings can be achieved quickly and in a very simple way

3. It's hard to understand for someone new to it.

Therefore, we also need to understand the meaning of the characters that need to be used in the regular

The escape character removes the meaning of a particular symbol.

. any one character

* Repeat one character before zero or more * s

\{n,m\consecutive n to m previous repeated characters

[ ] character set

.* Match all characters

^matches begin with a character

$matches to end with a character

[^] negated

^.* Matches multiple arbitrary character starts

|or

[A-Z]26 capital letters.

[a-z]26 lowercase letters

[0-9]0 to 9 numbers

[A-Za-z0-9] 26 upper case letters, 26 lower case letters and numbers 0 to 9

\d matches a numeric character

\w Matches any word character including underscore

grep text search tool

grep parameter lookup criteria file name

Main parameters:

-c: Output only the count of matching rows

-I: Does not distinguish between upper and lower case (only applies to single characters)

-l: When querying multiple files, only output file names containing matching characters

-n: Display matching rows and row numbers

-s: Do not display error messages for non-existent or no matching text

-v: Display all lines that do not contain matching text

sed online editing tool

[root@Centos bbs]# man sed

NAME

sed - stream editor for filtering and transforming text

Flow editor for filtering or converting text

sed parameter action

Main parameters:

-n: Cancel default output

-i: modify file content

Action:

p print

D delete

s#x#xx#g Substitution and Global Substitution Global substitution of X by XX

awk text processing tools

[root@Centos /]# man awk

NAME

gawk - pattern scanning and processing language

awk 'Condition 1 {Action 1} Condition 2{Action 2}......................... 'File name

-F Use delimiter

== equals

NR line fetch

Grep, sed, awk parameters are very many, we are interested in slowly study, check the help documentation

exemplified

Example 1: Print out the IP address of eth0 in the information listed by the ifconfig command

That is to say, print 192.168.1.2 on the screen with the command

Method 1:

[root@Centos /]# ifconfig eth0 |grep "inet addr"

inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0

First filter the lines with IP addresses and print them out, then filter them to extract IP addresses.

[root@Centos /]# ifconfig eth0 |grep "inet addr"|awk -F '[ :]+' '{print $4}'

192.168.1.2

Use spaces and: as separators,+ represents the previous multiple repeated separators as one, print the fourth column

Method 2:

[root@Centos /]# ifconfig eth0

eth0 Link encap:Ethernet HWaddr 00:0C:29:AA:CA:2B

inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:feaa:ca2b/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:7005 errors:0 dropped:0 overruns:0 frame:0

TX packets:3095 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:709421 (692.7 KiB) TX bytes:454116 (443.4 KiB)

[root@Centos /]# ifconfig eth0| awk -F '[ :]+' NR==2 Take the entire second line

inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0

[root@Centos /]# ifconfig eth0| awk -F '[ :]+''NR==2 {print $4}' Print fourth column

192.168.1.2

Method 3:

[root@Centos /]# ifconfig eth0

eth0 Link encap:Ethernet HWaddr 00:0C:29:AA:CA:2B

inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:feaa:ca2b/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:7271 errors:0 dropped:0 overruns:0 frame:0

TX packets:3244 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:733402 (716.2 KiB) TX bytes:473258 (462.1 KiB)

[root@Centos /]# ifconfig eth0| sed -n 2p Print the second line

inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0

[root@Centos /]# ifconfig eth0|sed -n 2p|sed 's#^.* addr:# #g'

192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0

will be marked with spaces.* (any character before) addr: Replace the contents at the beginning with empty output

[root@Centos /]# ifconfig eth0|sed -n 2p|sed 's#^.* addr:# #g'|sed 's# Bcast.*$# #g'will start with two spaces Bcast.* (Any character after) Replace the content at the end with empty output

192.168.1.2

Method 4:

[root@Centos /]# ifconfig eth0

eth0 Link encap:Ethernet HWaddr 00:0C:29:AA:CA:2B

inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:feaa:ca2b/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:7723 errors:0 dropped:0 overruns:0 frame:0

TX packets:3434 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:776543 (758.3 KiB) TX bytes:493886 (482.3 KiB)

[root@Centos /]# ifconfig eth0|sed -n 's#^.* addr:\(.*\) Bcast.*$#\ 1#gp'

192.168.1.2

Match the desired content to (), and then print out the content in ()

Example 2: Convert the permissions of the etc directory under/directory into digital output

Method 1:

[root@Centos /]# stat /etc stat Get attribute information for a file or directory

File: `/etc'

Size: 12288 Blocks: 24 IO Block: 4096 directory

Device: fd00h/64768d Inode: 2883585 Links: 116

Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2016-08-21 11:07:01.802852859 +0800

Modify: 2016-08-21 11:06:44.569852839 +0800

Change: 2016-08-21 11:06:44.569852839 +0800

[root@Centos /]# stat /etc|sed -n 4p

Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Print fourth line

[root@Centos /]# stat /etc| sed -n 4p| cut -d "(" -f2 Print second column with (as separator

0755/drwxr-xr-x) Uid:

[root@Centos /]# stat /etc|sed -n 4p|cut -d "(" -f2|cut -d "/" -f1

0755 Print second column with/as separator

Method 2:

[root@Centos /]# stat /etc|sed -n 4p|cut -d "(" -f2|cut -c 2-4

755 Take the second column with (delimiter, then 2-4 characters

Method 3:

[root@Centos /]# stat /etc|sed -n 4p|awk -F '(' '{print $2}'|awk -F '/' '{print $1}'

0755 Print second column with (separator| Print first column with/separator

Method 4:

[root@Centos /]# stat /etc| sed -n 4p Print the fourth line

Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)

[root@Centos /]# stat /etc| sed -n 4p| awk -F '[(/]'{print $2}'Specify multiple delimiters

0755

Method 5: In fact, the stat command itself has parameters that can be obtained directly

[root@Centos /]# stat -c %a /etc

755

[root@Centos /]# stat -c %a /root

550

[root@Centos /]# stat -c %a /tmp

1777

About how to look at Linux system regular expressions to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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