第三周 DAY3老男孩linux運(yùn)維58期學(xué)員七組許金龍之課堂筆記

3W1H:

1、什么是正則表達(dá)式?

作用和特殊字符一樣。

正則表達(dá)式是為處理大量的字符串及文本而定義的一套規(guī)則和方法。

開(kāi)發(fā)者

假設(shè)"@"代表“I am”,"!"代表“oldboy”,

則執(zhí)行echo "@!"的結(jié)果就是輸出“I am oldboy”。

發(fā)明語(yǔ)言:

上了火星,發(fā)明火星語(yǔ)。

! 我喜歡你

@? 滾

2、提高效率,快速獲取到想要的內(nèi)容。

3、適用于三劍客命令 grep(egrep),sed,awk

? 以行為單位處理。

4、實(shí)踐來(lái)講解

易混淆事項(xiàng)

1、和通配符區(qū)別。

2、開(kāi)發(fā)人員正則,一般是Perl兼容正則表達(dá)式。

3、Linux系統(tǒng)三劍客正則表達(dá)式******。

環(huán)境準(zhǔn)備:

export LC_ALL=C

分類(lèi):

1、BRE? grep

2、ERE? egrep

[root@oldboyedu ~/test]# grep "^I" oldboy.txt

I am oldboy teacher!

I teach linux.

I like badminton ball ,billiard ball and chinese chess!

[root@oldboyedu ~/test]# ls /data

a.txt? aa.txt? b.txt? c.txt? f.txt? test.txt

[root@oldboyedu ~/test]# mkdir /data/oldboy

[root@oldboyedu ~/test]# ls -l /data

total 0

-rw-r--r--. 1 root root 0 Oct? 4 23:28 a.txt

-rw-r--r--. 1 root root 0 Oct? 4 23:28 aa.txt

-rw-r--r--. 1 root root 0 Oct? 4 23:28 b.txt

-rw-r--r--. 1 root root 0 Oct? 4 23:38 c.txt

-rw-r--r--. 1 root root 0 Oct? 4 23:38 f.txt

drwxr-xr-x. 2 root root 6 Oct? 5 01:08 oldboy

-rw-r--r--. 1 root root 0 Oct? 4 23:28 test.txt

[root@oldboyedu ~/test]# ls -l /data|grep "^d"

drwxr-xr-x. 2 root root 6 Oct? 5 01:08 oldboy

es?? 匹配e es

es*? 匹配e es ess essssss essssssssss 更多s。

?0

0?

代表 自身是 .代表任意一個(gè)字符。

匹配 ? 匹配前一個(gè)字符。

e*s* 和e?s?

e*? 空 e ee eeee eeeeee

s*? 空 s ss sss ssssssssssssss

e*s* 空 es e s ees essssss

e?s? 空 e s es

e? 空 e

s? 空 s

=========================================

e+? ? e ee eee eeeee? .........

e*? 空 e ee eeee eeeeee .........

e?? 空 e

=========================================

a{n,m} 匹配前一個(gè)字符最少n次,最多m次

a{n,} 匹配前一個(gè)字符最少n次

a{n} 匹配前一個(gè)字符正好n次

a{,m} 匹配前一個(gè)字符最多m次

(0)===\1

(0)(0)? ? \1? ? ? ? ? ? \2

? ? ? 第一個(gè)括號(hào)? ? 第二個(gè)括號(hào)

egrep -o "(e)(s)\1\2" oldboy.txt ===== egrep -o "eses" oldboy.txt

特殊預(yù)定義中括號(hào)表達(dá)式

[root@oldboyedu ~/test]# egrep "[0-9]" oldboy.txt

my qq num is 49000448.

not 4900000448.

[root@oldboyedu ~/test]#

[root@oldboyedu ~/test]# egrep "[[:digit:]]" oldboy.txt

my qq num is 49000448.

not 4900000448.

[root@oldboyedu ~/test]# egrep "[[:lower:]]" oldboy.txt

I am oldboy teacher!

I teach linux.

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldboyedu.com

my qq num is 49000448.

not 4900000448.

my god ,i am not oldbey,but OLDBOY!

[root@oldboyedu ~/test]# egrep "[[:upper:]]" oldboy.txt

I am oldboy teacher!

I teach linux.

I like badminton ball ,billiard ball and chinese chess!

my god ,i am not oldbey,but OLDBOY!

[root@oldboyedu ~/test]# egrep "\boldboy\b" oldboy.txt

I am oldboy teacher!

[root@oldboyedu ~/test]# egrep "oldboy" oldboy.txt

I am oldboy teacher!

our site is http://www.oldboyedu.com

[root@oldboyedu ~/test]# egrep -w "oldboy" oldboy.txt

I am oldboy teacher!

評(píng)書(shū):三俠劍? 老好了。

俠客、劍客

Linux三劍客

awk? sed? grep

sed

Sed是操作、過(guò)濾和轉(zhuǎn)換文本內(nèi)容的強(qiáng)大工具。

常用功能有對(duì)文件實(shí)現(xiàn)快速增刪改查(增加、刪除、修改、查詢),

其中查詢的功能中最常用的2大功能是過(guò)濾(過(guò)濾指定字符串)和取行(取出指定行)。

sed [選項(xiàng)]? [sed內(nèi)置命令字符]? [文件]

選項(xiàng):

-n 取消默認(rèn)sed的輸出,常與sed內(nèi)置命令的p連用※

-i 直接修改文件內(nèi)容,而不是輸出到終端。

如果不使用-i選項(xiàng)sed只是修改在內(nèi)存中的數(shù)據(jù),并不會(huì)影響磁盤(pán)上的文件※

sed的內(nèi)置命令字符說(shuō)明

s 替換

g 全局global

p 打印print

d 刪除delete

I am oldboy teacher!

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldboyedu.com

my qq num is 49000448.

[root@oldboyedu ~/test]# cat oldgirl.txt

I am oldboy teacher!

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldboyedu.com

my qq num is 49000448.

問(wèn)題1:輸出oldboy.txt的第2-3行內(nèi)容※。

sed -n '2,3p' oldgirl.txt

問(wèn)題2:過(guò)濾出含有oldboy字符串的行※。

sed -n '/oldboy/p' oldgirl.txt

問(wèn)題3:刪除含有oldboy字符串的行※。

sed '/oldboy/d' oldgirl.txt

sed /oldboy/d oldgirl.txt

問(wèn)題4:將文件中的oldboy字符串全部替換為oldgirl※。

vim替換:

:%s#oldboy#oldgirl#g

sed 's#想替換啥#用啥替換#g' oldgirl.txt

sed 's#oldboy#oldgirl#g' oldgirl.txt

修改文件:

sed -i 's#oldboy#oldgirl#g' oldgirl.txt

問(wèn)題5:將文件中的oldboy字符串全部替換為oldgirl,同時(shí)將QQ號(hào)碼49000448改為31333741。

sed -e 's#oldboy#oldgirl#g' -e 's#49000448#31333741#g' oldgirl.txt I

環(huán)境:

[root@oldboyedu ~/test]# cat oldgirl.txt

I am oldboy teacher!

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldboyedu.com

my qq num is 49000448.

問(wèn)題1:輸出oldboy.txt的第2-3行內(nèi)容※。

問(wèn)題2:過(guò)濾出含有oldboy字符串的行※。

問(wèn)題3:刪除含有oldboy字符串的行※。

問(wèn)題4:將文件中的oldboy字符串全部替換為oldgirl※。

問(wèn)題5:將文件中的oldboy字符串全部替換為oldgirl,同時(shí)將QQ號(hào)碼49000448改為31333741。

問(wèn)題6:在oldboy.txt文件的第2行后追加文本。

問(wèn)題7:在oldboy.txt文件的第2行插入文本。

刪除指定行

sed -i '3d' oldgirl.txt

sed -i '5,8d' oldgirl.txt

環(huán)境:

[root@oldboyedu ~/test]# cat oldgirl.txt

I am oldboy teacher!

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldboyedu.com

my qq num is 49000448.

問(wèn)題1:輸出oldboy.txt的第2-3行內(nèi)容※。

[root@oldboyedu ~/test]# sed -n '2,3p' oldgirl.txt

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldboyedu.com

[root@oldboyedu ~/test]# head -3 oldgirl.txt |tail -2

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldboyedu.com

問(wèn)題2:過(guò)濾出含有oldboy字符串的行※。

[root@oldboyedu ~/test]# sed -n '/oldboy/p' oldgirl.txt

I am oldboy teacher!

our site is http://www.oldboyedu.com

[root@oldboyedu ~/test]# grep oldboy oldgirl.txt

I am oldboy teacher!

our site is http://www.oldboyedu.com

問(wèn)題3:刪除含有oldboy字符串的行※。

[root@oldboyedu ~/test]# sed '/oldboy/d' oldgirl.txt

I like badminton ball ,billiard ball and chinese chess!

my qq num is 49000448.

[root@oldboyedu ~/test]# grep -v "oldboy" oldgirl.txt

I like badminton ball ,billiard ball and chinese chess!

my qq num is 49000448.

問(wèn)題4:將文件中的oldboy字符串全部替換為oldgirl※。

[root@oldboyedu ~/test]# sed 's#oldboy#oldgirl#g' oldgirl.txt

I am oldgirl teacher!

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldgirledu.com

my qq num is 49000448.

vim替換:

問(wèn)題5:將文件中的oldboy字符串全部替換為oldgirl,同時(shí)將QQ號(hào)碼49000448改為31333741。

[root@oldboyedu ~/test]# sed -e 's#oldboy#oldgirl#2' -e 's#49000448#31333741#g' oldgirl.txt

I am oldboy teacher!

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldboyedu.com

my qq num is 31333741.

問(wèn)題6:在oldboy.txt文件的第2行后追加文本。

[root@oldboyedu ~/test]# sed '2a I teacher linux.' oldgirl.txt

I am oldboy teacher!

I like badminton ball ,billiard ball and chinese chess!

I teacher linux.

our site is http://www.oldboyedu.com

my qq num is 49000448.

[root@oldboyedu ~/test]# cat oldgirl.txt

I am oldboy teacher!

I like badminton ball ,billiard ball and chinese chess!

our site is http://www.oldboyedu.com

my qq num is 49000448.

[root@oldboyedu ~/test]# sed -i '2a I teacher linux.' oldgirl.txt

[root@oldboyedu ~/test]# cat oldgirl.txt

I am oldboy teacher!

I like badminton ball ,billiard ball and chinese chess!

I teacher linux.

our site is http://www.oldboyedu.com

my qq num is 49000448.

[root@oldboyedu ~/test]# sed '2i I teacher linux.i' oldgirl.txt

I am oldboy teacher!

I teacher linux.i

I like badminton ball ,billiard ball and chinese chess!

I teacher linux.

our site is http://www.oldboyedu.com

my qq num is 49000448.

問(wèn)題1:取出Linux中執(zhí)行ifconfig eth0后對(duì)應(yīng)的IP地址(只能輸出IP地址)。

練習(xí)sed

正則是貪婪匹配模式,人性是貪婪的。

方法1:

[root@oldboyedu ~/test]# ifconfig eth0|sed -n 2p|sed 's#^.*inet ##g'|sed 's#? netm.*$##g'

10.0.0.201

方法:要取一個(gè)目標(biāo),刪除目標(biāo)兩邊的,就得到了目標(biāo):

先匹配上,然后在刪除

[root@oldboyedu ~]# ifconfig eth0|sed -n 2p|sed 's#^.*inet ##g'|sed 's#? netm.*$##g'

10.0.0.201

[root@oldboyedu ~]# ifconfig eth0|sed -n 2p|sed -e 's#^.*inet ##g' -e 's#? netm.*$##g'

10.0.0.201

[root@oldboyedu ~]# ifconfig eth0|sed -ne 's#^.*inet ##g' -e 's#? netm.*$##gp'

10.0.0.201

[root@oldboyedu ~]# ifconfig eth0|sed -nr '2s#^.*inet (.*)? netm.*$#\1#gp'

10.0.0.201

驗(yàn)證是否懂了的練習(xí)題:

stat /etc/hosts自行取其中的644

[root@oldboyedu ~]# stat /etc/hosts

? File: ‘/etc/hosts’

? Size: 158? ? ? Blocks: 8? ? ? ? ? IO Block: 4096? regular file

Device: 803h/2051d Inode: 16829878? ? Links: 1

Access: (0644/-rw-r--r--)? Uid: (? ? 0/? ? root)? Gid: (? ? 0/? ? root)

Access: 2020-10-06 20:40:18.463001804 +0800

Modify: 2013-06-07 22:31:32.000000000 +0800

Change: 2019-03-04 11:15:49.426692303 +0800

Birth: -

[root@oldboyedu ~]# stat /etc/hosts|sed -rn 's#^.*\(0(.*)/-.*$#\1#gp'

644

考試:ip add的輸出取出IP。

[root@oldboyedu ~]# ip add|sed -rn 's#^.*net (.*)/24.*$#\1#gp'

10.0.0.201

1、象棋,教學(xué),比賽,活動(dòng)

2、踢毽子,比賽。最牛的單人前三,小組前三。

三劍客自身有特長(zhǎng)的。

grep 過(guò)濾查找內(nèi)容。篩子

sed? 取行,替換,刪除,追加

awk? 取列

cut 按列切割

-d 指定分隔符 -f指定哪列,多列用逗號(hào)

[root@oldboyedu ~]# cat a.txt

1 2 3 4 5 6 7 8 9 10

[root@oldboyedu ~]# cut -d" " -f1,3,5 a.txt

1 3 5

[root@oldboyedu ~]# cut -d" " -f3-5 a.txt

3 4 5

練習(xí)

[root@oldboyedu ~]# sed -n '1,5p' /etc/passwd >oldboyedu.txt

[root@oldboyedu ~]# cat oldboyedu.txt

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

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[root@oldboyedu ~]# cut -d":" -f3,4 /etc/passwd

0:0

1:1

2:2

[root@oldboyedu ~]# cat b.txt

oldboy 49000448

[root@oldboyedu ~]# cut -c1-6,8- b.txt

oldboy4900044

awk語(yǔ)法:

awk? [option]? 'pattern{action}' file ...

awk? [參數(shù)]? ? ? '條件{動(dòng)作}'? ? 文件 ...

參數(shù):

-F 指定分隔符

打印第一列:

[root@oldboyedu ~]# awk -F ":" '{print $1}' oldboyedu.txt

root

bin

daemon

adm

lp

[root@oldboyedu ~]# awk -F ":" '{print $3,$5}' oldboyedu.txt

0 root

1 bin

2 daemon

3 adm

4 lp

列:$1第一列 $2第二列 以此類(lèi)推....

$0? 整行

$NF 最后一列 倒數(shù)第一列

$(NF-1) 倒數(shù)第二列

[root@oldboyedu ~]# awk -F ":" '{print $NF}' oldboyedu.txt

/bin/bash

/sbin/nologin

/sbin/nologin

/sbin/nologin

/sbin/nologin

[root@oldboyedu ~]# awk -F ":" '{print $0}' oldboyedu.txt

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

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

練習(xí):打印 1 2 和最后一列。

[root@oldboyedu ~]# awk -F ":" '{print $1,$2,$NF}' oldboyedu.txt

root x /bin/bash

bin x /sbin/nologin

daemon x /sbin/nologin

adm x /sbin/nologin

lp x /sbin/nologin

問(wèn)題1:取test.txt文件的第2行到第3行的內(nèi)容。

[root@oldboyedu ~]# awk 'NR>1&&NR<4' oldboyedu.txt

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

[root@oldboyedu ~]# awk 'NR==2,NR==3' oldboyedu.txt

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

問(wèn)題2:過(guò)濾出含有root字符串的行※。

[root@oldboyedu ~]# awk '/root/' oldboyedu.txt

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

[root@oldboyedu ~]# awk /root/ oldboyedu.txt

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

[root@oldboyedu ~]#

[root@oldboyedu ~]# awk "/root/" oldboyedu.txt

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

問(wèn)題3:刪除含有root字符串的行※。

[root@oldboyedu ~]# awk '/^[^r]/' oldboyedu.txt

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[^r]? 非r

^[^r] 以非r字符卡頭

問(wèn)題4:取文件的第一列、第三列和最后一列內(nèi)容,并打印行號(hào)※。

[root@oldboyedu ~]# awk -F ":" '{print NR,$1,$3,$NF}' oldboyedu.txt

1 root 0 /bin/bash

2 bin 1 /sbin/nologin

3 daemon 2 /sbin/nologin

4 adm 3 /sbin/nologin

5 lp 4 /sbin/nologin

問(wèn)題5:取出Linux中執(zhí)行ifconfig eth0后對(duì)應(yīng)的IP地址(只能輸出IP地址)。

[root@oldboyedu ~]# ifconfig eth0|awk 'NR==2{print $2}'

10.0.0.201

C6

[root@oldboy ~]# ifconfig eth0

eth0? ? ? Link encap:Ethernet? HWaddr 00:0C:29:D0:87:20?

? ? ? ? ? inet addr:10.0.0.202? Bcast:10.0.0.255? Mask:255.255.255.0

? ? ? ? ? inet6 addr: fe80::20c:29ff:fed0:8720/64 Scope:Link

? ? ? ? ? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1

? ? ? ? ? RX packets:243 errors:0 dropped:0 overruns:0 frame:0

? ? ? ? ? TX packets:67 errors:0 dropped:0 overruns:0 carrier:0

? ? ? ? ? collisions:0 txqueuelen:1000

? ? ? ? ? RX bytes:20674 (20.1 KiB)? TX bytes:8706 (8.5 KiB)

[root@oldboy ~]# ifconfig eth0|awk 'NR==2{print $2}'

addr:10.0.0.202

[root@oldboy ~]# ifconfig eth0|awk 'NR==2{print $2}'|awk -F ":" '{print $2}'

10.0.0.202

[root@oldboy ~]# ifconfig eth0|awk -F "[: ]+" 'NR==2{print $4}'

10.0.0.202

問(wèn)題6:過(guò)濾文件中第一列內(nèi)容匹配root的字符串,把符合的行的最后一列輸出

awk -F ":" '$1~/root/ {print $NF}' test.txt

~匹配

問(wèn)題1:取test.txt文件的第2行到第3行的內(nèi)容。

問(wèn)題2:過(guò)濾出含有root字符串的行※。

問(wèn)題3:刪除含有root字符串的行※。

問(wèn)題4:取文件的第一列、第三列和最后一列內(nèi)容,并打印行號(hào)※。

問(wèn)題5:取出Linux中執(zhí)行ifconfig eth0后對(duì)應(yīng)的IP地址(只能輸出IP地址)。

問(wèn)題6:過(guò)濾文件中第一列內(nèi)容匹配root的字符串,把符合的行的最后一列輸出

問(wèn)題7:過(guò)濾下列test1.txt文件中第三列內(nèi)容分?jǐn)?shù)大于70,并且小于95的人名和性別。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容