3W1H:
1、什么是正則表達(dá)式晚唇?
作用和特殊字符一樣巫财。
正則表達(dá)式是為處理大量的字符串及文本而定義的一套規(guī)則和方法。
開發(fā)者
假設(shè)"@"代表“I am”哩陕,"!"代表“oldboy”平项,
則執(zhí)行echo "@!"的結(jié)果就是輸出“I am oldboy”。
發(fā)明語言:
上了火星悍及,發(fā)明火星語闽瓢。
! 我喜歡你
@? 滾
2心赶、提高效率鸳粉,快速獲取到想要的內(nèi)容。
3园担、適用于三劍客命令 grep(egrep),sed,awk
? 以行為單位處理。
4枯夜、實(shí)踐來講解
易混淆事項(xiàng)
1弯汰、和通配符區(qū)別。
2湖雹、開發(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è)字符癣籽。
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)書:三俠劍? 老好了。
俠客筷狼、劍客
Linux三劍客
awk? sed? grep
sed
Sed是操作瓶籽、過濾和轉(zhuǎn)換文本內(nèi)容的強(qiáng)大工具。
常用功能有對(duì)文件實(shí)現(xiàn)快速增刪改查(增加埂材、刪除塑顺、修改、查詢)俏险,
其中查詢的功能中最常用的2大功能是過濾(過濾指定字符串)和取行(取出指定行)严拒。
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)置命令字符說明
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.
問題1:輸出oldboy.txt的第2-3行內(nèi)容※预鬓。
sed -n '2,3p' oldgirl.txt
問題2:過濾出含有oldboy字符串的行※巧骚。
sed -n '/oldboy/p' oldgirl.txt
問題3:刪除含有oldboy字符串的行※。
sed '/oldboy/d' oldgirl.txt
sed /oldboy/d oldgirl.txt
問題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
問題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.
問題1:輸出oldboy.txt的第2-3行內(nèi)容※顶猜。
問題2:過濾出含有oldboy字符串的行※沧奴。
問題3:刪除含有oldboy字符串的行※。
問題4:將文件中的oldboy字符串全部替換為oldgirl※长窄。
問題5:將文件中的oldboy字符串全部替換為oldgirl滔吠,同時(shí)將QQ號(hào)碼49000448改為31333741。