3月19號(hào)
什么是正則表達(dá)式?
作用和特殊字符一樣。
正則表達(dá)式是為處理大量的字符串及文本而定義的一套規(guī)則和方法。
開發(fā)者
假設(shè)“@”代表“I am”,“!”代表“oldboy”,
則這姓echo“@!”的結(jié)果就是輸出I am oldboy
2、提高效率,快速獲取到想要的內(nèi)容。
1、提高效率,快速獲取到想要的內(nèi)容。
2、三劍客命令grep(egrep),sed,awk
以行為單位處理
3、實(shí)踐來講解
易混淆的事項(xiàng)
1.和通配符區(qū)別
2.開發(fā)正則,一般是perl兼容正則表達(dá)式。
3.Linux三劍客正則表達(dá)式
export LC_ALL=C
分類:
1.BRE基本正則表達(dá)式 ?(grep)
2. Ere擴(kuò)展正則表達(dá)式 ?(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
特殊預(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!
三劍客:grep ?sed(Stream editor 流編輯器)awd
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ì)影響磁盤上的文件※
ed的內(nèi)置命令字符說明
s替換
g全局global
p打印print
d刪除delete
問題1:輸出oldboy.txt的第2-3行內(nèi)容※。
root@guohaoxiang~/test]# sed -n '2,3p' oldboy.txt #<==n
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
問題2;過濾出含有oldboy的字符
[root@guohaoxiang~/test]# sed -n '/oldboy/p' oldboy.txt
I am oldboy teacher!
our site is http://www.oldboyedu.com
問題3:刪除含有oldboy字符串的行
[root@guohaoxiang~/test]# sed '/oldboy/d' oldboy.txt
I like badminton ball ,billiard ball and chinese chess!
my qq num is 49000448.
問題3:將文件中的oldboy字符串全部替換為oldgirl
Vim替換:
sed?"s/oldboy/oldgirl/ig"?test.txt?
Sed ‘s#像替換什么#用什么替換#g’ oldgirl.Txt
問題4:將文件zho將文件中的oldboy字符串全部替換為oldgirl同時(shí)將qq號(hào)碼49000448改為12345678
[root@guohaoxiang~/test]# sed -e 's#oldboy#oldgirl#g' -e 's#49000448#12340569#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 12345678.