Day15 課堂筆記
1. 正則表達(dá)式
什么是正則表達(dá)式?
簡(jiǎn)單地說(shuō),正則表達(dá)式就是為了處理大量的字符串及文本而定義的一套規(guī)則和方法。假設(shè)"@"代表"I am","!"代表"oldboy",則執(zhí)行echo "@!"的結(jié)果就是輸出"I am oldboy"。通過(guò)這些特殊符號(hào)的輔助,管理員就可以快速過(guò)濾、替換或輸出需要的字符串。
linux三劍客的正則表達(dá)式有以下幾個(gè)特點(diǎn):
- 為處理大量的文本及字符串而定義的一套規(guī)則和方法。
- 其工作時(shí)以行為單位進(jìn)行,即一次處理一行。
- 通過(guò)正則表達(dá)式可以將復(fù)雜的處理任務(wù)化繁為簡(jiǎn),提高操作linux的效率。
- 僅被三劍客(grep/egrep、sed、awk)命令支持,其他命令無(wú)法使用。
為什么要學(xué)習(xí)正則表達(dá)式?
實(shí)際企業(yè)中,運(yùn)維工程師在做linux運(yùn)維工作時(shí),通常都會(huì)面對(duì)大量的帶有字符串的內(nèi)容,比如文本配置、程序、命令輸出及日志文件等,我們要從大量的字符串內(nèi)容中查找符合工作需要的特定的字符串。這就需要靠正則表達(dá)式了。
有關(guān)正則表達(dá)式容易混淆的事項(xiàng)!
正則表達(dá)式的應(yīng)用非常廣泛,在這里,我們所講的是linux系統(tǒng)運(yùn)維工作中的正則表達(dá)式,就是linux三劍客
grep(egrep)、sed、awk。
學(xué)習(xí)正則表達(dá)式注意事項(xiàng)。
1、注意LC_ALL環(huán)境變量的設(shè)置:
export LC_ALL=C
2、給grep和egrep設(shè)置別名(CentOS6需要,CentOS67不需要)
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
完整的處理及生效命令為:
cat >>/etc/profile<<EOF
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
export LC_ALL=C
EOF
source /etc/profile
2 基本與擴(kuò)展正則表達(dá)式集合

3 基本正則表達(dá)式實(shí)踐
[root@oldboyedu ~]# mkdir ~/test -p
[root@oldboyedu ~]# cat >~/test/oldboy.txt<<EOF
> 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!
> EOF
最終文件如下:
[root@oldboyedu ~]# cd ~/test/
[root@oldboyedu ~/test]# cat -n oldboy.txt
1 I am oldboy teacher!
2 I teach linux.
3
4 I like badminton ball ,billiard ball and chinese chess!
5 our site is http://www.oldboyedu.com
6 my qq num is 49000448.
7
8 not 4900000448.
9 my god ,i am not oldbey,but OLDBOY!
1 ^(尖角號(hào))功能實(shí)踐
[root@oldboyedu ~/test]# grep -n "^m" oldboy.txt
6:my qq num is 49000448.
9:my god ,i am not oldbey,but OLDBOY!
2 $(美元符)功能實(shí)踐
[root@oldboyedu ~/test]# grep -n "m$" oldboy.txt
5:our site is http://www.oldboyedu.com
默認(rèn)情況下,linux下的所有的文件結(jié)尾都有一個(gè)$符,查看如下:
root@oldboyedu ~/test]# grep -n "m$" oldboy.txt | cat -A
5:our site is http://www.oldboyedu.com$
3 ^$功能實(shí)踐
[root@oldboyedu ~/test]# grep -n "^$" oldboy.txt
3:
7:
4 .(點(diǎn))功能實(shí)踐
[root@oldboyedu ~/test]# grep -n "." oldboy.txt
1:I am oldboy teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448.
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!
5 \(轉(zhuǎn)義符)功能實(shí)踐
[root@oldboyedu ~/test]# grep -n "\.$" oldboy.txt
2:I teach linux.
6:my qq num is 49000448.
8:not 4900000448.
6 *(星號(hào))功能實(shí)踐
[root@oldboyedu ~/test]# grep -n "0*" oldboy.txt
1:I am oldboy teacher!
2:I teach linux.
3:
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448.
7:
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!
7 .*組合符功能實(shí)踐
[root@oldboyedu ~/test]# grep -n ".*" oldboy.txt
1:I am oldboy teacher!
2:I teach linux.
3:
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448.
7:
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu ~/test]# grep -n "^.*o" oldboy.txt
1:I am oldboy teacher!
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!
8 [](中括號(hào))功能實(shí)踐
[root@oldboyedu ~/test]# grep -n '[A-Z]' oldboy.txt
1:I am oldboy teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
9:my god ,i am not oldbey,but OLDBOY!
9 [^abc](中括號(hào)內(nèi)取反符)功能實(shí)踐
[root@oldboyedu ~/test]# grep -n '[^a-z]' oldboy.txt
1:I am oldboy teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448.
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!```
4 擴(kuò)展正則表達(dá)式實(shí)踐
1 +(加號(hào))功能實(shí)踐
[root@oldboyedu ~/test]# egrep "0+" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboyedu ~/test]# egrep -o "0+" oldboy.txt
000
00000
2 ?(問(wèn)號(hào))功能實(shí)踐
[root@oldboyedu ~/test]# cat >oldgril.txt <<EOF
> good
> glad
> gd
> god
> good
> EOF
[root@oldboyedu ~/test]# egrep 'go?d' oldgril.txt
gd
god
3 |(豎線)功能實(shí)踐
[root@oldboyedu ~/test]# egrep '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
4 ()(小括號(hào))功能實(shí)踐
1.) 分組功能實(shí)踐
要求:取出包含good或glad的行
不用小括號(hào)()時(shí):
[root@oldboyedu ~/test]# cat oldgril.txt
good
glad
gd
god
good
goood
[root@oldboyedu ~/test]# egrep 'goo|lad' oldgril.txt
good
glad
good
goood
多了一個(gè)goood,不準(zhǔn)確,那么用小括號(hào)試試:
[root@oldboyedu ~/test]# cat oldgril.txt
good
glad
gd
god
good
goood
[root@oldboyedu ~/test]# egrep 'g(oo|la)d' oldgril.txt
good
glad
good
2.) 小括號(hào)的后引向功能實(shí)踐
[root@oldboyedu ~/test]# cat oldgril.txt
good
glad
gd
god
good
goood
[root@oldboyedu ~/test]# egrep "(o)\1" oldgril.txt
good
good
goood
5 {n,m}匹配次數(shù)功能實(shí)踐
[root@oldboyedu ~/test]# egrep "0{3,5}" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboyedu ~/test]# egrep "0{,5}" 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 "0{3,}" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboyedu ~/test]# egrep "0{3}" oldboy.txt
my qq num is 49000448.
not 4900000448.
5 sed :流編輯器(Linux三劍客之一)
sed是操作、過(guò)濾和轉(zhuǎn)換文本內(nèi)容的強(qiáng)大工具。
常用功能有對(duì)文件實(shí)現(xiàn)快速增刪改查(增加、刪除、修改、查詢),
其中查詢的功能中最常用的2大功能是過(guò)濾(過(guò)濾指定字符串)和取行(取出指定行)。
語(yǔ)法:
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
5.1 sed命令實(shí)踐
準(zhǔn)備測(cè)試文件:
[root@oldboyedu ~/test]# cat -n oldboy.txt
1 I am oldboy teacher!
2 I like badminton ball ,billiard ball and chinese chess!
3 our site is http://www.oldboyedu.com
4 my qq num is 49000448.
問(wèn)題1:輸出oldboy.txt的第2-3行內(nèi)容。
[root@oldboyedu ~/test]# sed -n '2,3p' oldboy.txt
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' oldboy.txt
I am oldboy teacher!
our site is http://www.oldboyedu.com
問(wèn)題3:刪除含有oldboy字符串的行。
[root@oldboyedu ~/test]# sed '/oldboy/d' oldboy.txt
I like badminton ball ,billiard ball and chinese chess!
my qq num is 49000448.
問(wèn)題4:將文件中的oldboy字符串全部替換為oldgirl。
vim替換:
:%s#oldboy#oldgirl#g
或者
[root@oldboyedu ~/test]# sed 's#oldboy#oldgirl#g' oldboy.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.
修改文件:
sed -i 's#oldboy#oldgirl#g' oldboy.txt
問(wèn)題5:將文件中的oldboy字符串全部替換為oldgirl,同時(shí)將QQ號(hào)碼49000448改為31333741。
sed -e 's#oldboy#oldgirl#g' -e 's#49000448#31333741#g' oldboy.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 31333741.
問(wèn)題6:刪除指定行,比如第3行或1-4行
sed -i '3d' oldboy.txt
sed -i '1,4d' oldboy.txt
問(wèn)題6:將abc整體(不區(qū)分大小寫(xiě))替換成123
[root@oldboyedu ~/test]# cat>abc.txt <<EOF
> abc
> ABc
> ABC
> aBc
> aAA
> BcA
> AAA
> EOF
[root@oldboyedu ~/test]# cat abc.txt
abc
ABc
ABC
aBc
aAA
BcA
AAA
[root@oldboyedu ~/test]# sed -i 's/abc/123/ig' abc.txt
[root@oldboyedu ~/test]# !cat
cat abc.txt
123
123
123
123
aAA
BcA
AAA
問(wèn)題7:在oldboy.txt文件的第二行的下一行追加文I teacher linux.
[root@oldboyedu ~/test]# cat -n oldboy.txt
1 I am oldboy teacher!
2 I like badminton ball ,billiard ball and chinese chess!
3 our site is http://www.oldboyedu.com
4 my qq num is 49000448.
[root@oldboyedu ~/test]# sed -i '2a I teacher linux.' oldboy.txt
[root@oldboyedu ~/test]# cat -n oldboy.txt
1 I am oldboy teacher!
2 I like badminton ball ,billiard ball and chinese chess!
3 I teacher linux.
4 our site is http://www.oldboyedu.com
5 my qq num is 49000448.
問(wèn)題8:在oldboy.txt文件的第二行的下一行追加文I teacher linux at 2i.
[root@oldboyedu ~/test]# cat -n oldboy.txt
1 I am oldboy teacher!
2 I like badminton ball ,billiard ball and chinese chess!
3 I teacher linux.
4 our site is http://www.oldboyedu.com
5 my qq num is 49000448.
[root@oldboyedu ~/test]# sed -i '2i I teacher linux at 2i.' oldboy.txt
[root@oldboyedu ~/test]# cat -n oldboy.txt
1 I am oldboy teacher!
2 I teacher linux at 2i.
3 I like badminton ball ,billiard ball and chinese chess!
4 I teacher linux.
5 our site is http://www.oldboyedu.com
6 my qq num is 49000448.
問(wèn)題9:取ip案例
ifconfig eth0|sed -n 2p|sed 's#^.*inet ##g'|sed 's# netm.*$##g'
10.0.0.201
或者
ifconfig eth0|sed -n 2p|sed -e 's#^.*inet ##g' -e 's# netm.*$##g'
10.0.0.201
或者
ifconfig eth0|sed -ne 's#^.*inet ##g' -e 's# netm.*$##gp'
10.0.0.201
或者
ifconfig eth0|sed -nr '2s#^.*inet (.*) netm.*$#\1#gp'
10.0.0.201
或者
ip add|sed -rn 's#^.*net (.*)/24.*$#\1#gp'
6 cut:按列切割
參數(shù):
- -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
[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
7 awk (Linux三劍客之一)
語(yǔ)法:
awk [option] 'pattern{action}' file ...
awk [參數(shù)] '條件{動(dòng)作}' 文件 ...
參數(shù):
- -F 指定分隔符
| 變量名 | 屬性 |
|---|---|
| $0 | 整行 |
| $n | 第n列(-F指定分隔符) |
| NF | 記錄列的個(gè)數(shù),就是有多少列 |
| $NF | 最后1列 |
| $(NF-1) | 倒數(shù)第2列 |
| NR | 顯示行號(hào) |
案例:
環(huán)境準(zhǔn)備:
[root@oldboyedu ~/test]# sed -n '1,5p' /etc/passwd > test.txt
[root@oldboyedu ~/test]# cat -n test.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
問(wèn)題1:取test.txt文件的第2行到第3行的內(nèi)容。
[root@oldboyedu ~/test]# awk 'NR>1&&NR<4' test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
或者
[root@oldboyedu ~/test]# awk 'NR==2,NR==3' test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
問(wèn)題2:過(guò)濾出含有root字符串的行。
[root@oldboyedu ~/test/]# awk '/root/' test.txt
root:x:0:0:root:/root:/bin/bash
[root@oldboyedu ~/test/]# awk /root/ test.txt
root:x:0:0:root:/root:/bin/bash
[root@oldboyedu ~/tstst/]# awk "/root/" test.txt
root:x:0:0:root:/root:/bin/bash
問(wèn)題3:刪除含有root字符串的行。
[root@oldboyedu ~/test/]# awk '/^[^r]/' test.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字符開(kāi)頭
問(wèn)題4:取文件的第一列、第三列和最后一列內(nèi)容,并打印行號(hào)。
[root@oldboyedu ~/test/]# awk -F ":" '{print NR,$1,$3,$NF}' test.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
問(wèn)題6:過(guò)濾文件中第一列內(nèi)容匹配root的字符串,把符合的行的最后一列輸出
[root@oldboyedu ~/test]# awk -F ":" '$1~/root/ {print $NF}' test.txt
/bin/bash