第十四天課堂筆記 2019-03-19
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
分類:
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è)字符。
es 和e?s?
e* 空 e ee eeee eeeeee
s* 空 s ss sss ssssssssssssss
es 空 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ì)影響磁盤上的文件※
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.
環(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.
sed的幾種重要用法
問(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)題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
常用的基本正則表達(dá)式特殊字符及功能說(shuō)明如下表:


基本正則和擴(kuò)展正則:
