正則表達(dá)式
- 關(guān)于正則表達(dá)式相信很多學(xué)計(jì)算機(jī)的人都聽說過
- 尤其是做編程行業(yè)的人
那什么是正則表達(dá)式
- 正則表達(dá)式行瑞,又稱規(guī)則表達(dá)式担映。(英語:Regular Expression拧粪,在代碼中常簡寫為regex灶搜、regexp或RE),計(jì)算機(jī)科學(xué)的一個(gè)概念瞎疼。正則表通常被用來檢索科乎、替換那些符合某個(gè)模式(規(guī)則)的文本
- 正則表達(dá)式的“鼻祖”或許可一直追溯到科學(xué)家對(duì)人類神經(jīng)系統(tǒng)工作原理,好了不啰嗦了本章是說怎么學(xué)正則表達(dá)式
- 我們本章說的是linux正則表達(dá)式的學(xué)習(xí)
- 想學(xué)正則表達(dá)式首先你得會(huì)文本命令如三劍客grep,sed,awk
- 還有文本一些重要的文本命令來相互結(jié)合才能說學(xué)會(huì)一點(diǎn)點(diǎn)
- 因?yàn)檎齽t表達(dá)式靈活性非常強(qiáng)大贼急,在編程界有百靈鳥之稱
- 那我們開始說說他開始怎么學(xué)吧茅茂,我們先從最簡單的開始
- 正則表達(dá)式是你定義的、Linux工具用來過濾文本的模式模板太抓。Linux工具(如grep空闲,egrep)能
夠在數(shù)據(jù)流向工具時(shí)對(duì)數(shù)據(jù)進(jìn)行正則表達(dá)式模式匹配。如果數(shù)據(jù)匹配模式走敌,它就會(huì)被接受并進(jìn)一
步處理碴倾。如果數(shù)據(jù)不匹配模式,它就會(huì)被過濾掉。示意圖如下:
文本處理工具
cat 命令
- 這個(gè)命令我們用的非常多跌榔,一般拿來查看文件异雁,但是他的選項(xiàng)我們到不是經(jīng)常用,這里給大家多啰嗦啰嗦說說他的選項(xiàng)僧须,也是為了加深我學(xué)習(xí)記憶
-A, --show-all 等價(jià)于 -vET
-b, --number-nonblank 對(duì)非空輸出行編號(hào)
-e 等價(jià)于 -vE
-E, --show-ends 在每行結(jié)束處顯示 $
-n, --number 對(duì)輸出的所有行編號(hào)
-s, --squeeze-blank 不輸出多行空行
-t 與 -vT 等價(jià)
-T, --show-tabs 將跳格字符顯示為 ^I
-v, --show-nonprinting 使用 ^ 和 M- 引用纲刀,除了 LFD 和 TAB 之外
--help 顯示此幫助信息并退出
--version 輸出版本信息并退出
[root@localhost ~]#cat test #普通輸出
1111111111
2222222222
3333333333
[root@localhost ~]#cat -n test #開頭顯示行號(hào)
1 1111111111
2
3
4 2222222222
5
6 3333333333
[root@localhost ~]#cat -E test #以$結(jié)束
1111111111$
$
$
2222222222$
$
3333333333$
[root@localhost ~]#cat -s test #超過二個(gè)空行,合并成一個(gè)
1111111111
2222222222
3333333333
[root@localhost ~]#cat -ns test #去空行担平,加行號(hào)
1 1111111111
2
3 2222222222
4
5 3333333333
[root@localhost ~]#cat x* > google_bak.tar.gz #合并文件
[root@localhost ~]#cat test.tar.gz_?? > test.tar.gz #可以用cat命令將被切割的多個(gè)壓縮包合并成一個(gè)
[root@localhost ~]#tar -xvzf test.tar.gz #再用tar命令解壓
[root@localhost ~]#cat > aa #從鍵盤錄入內(nèi)容到文件示绊,回車是保存,退出Ctrl+z
4234234
234234
[root@localhost ~]#cat file1 file2 > file #合并二個(gè)文件為一個(gè)
tail 命令
-c, --bytes=N 輸出最后N個(gè)字節(jié)
-f, --follow[={name|descriptor}] 當(dāng)文件增長時(shí),輸出后續(xù)添加的數(shù)據(jù)
-n, --lines=N 輸出最后N行,而非默認(rèn)的最后10行
--pid=PID 與-f合用,表示在進(jìn)程ID,PID死掉之后結(jié)束.
-q, --quiet, --silen 從不輸出給出文件名的首部
-s, --sleep-interval=S 與-f合用,表示在每次反復(fù)的間隔休眠S秒
-v, --verbose 總是輸出給出文件名的首部
--help 顯示幫助信息后退出
--version 輸出版本信息后退出
[root@localhost ~]#tail /etc/passwd 默認(rèn)驱闷,顯示最后10 行耻台。
[root@localhost ~]#tail -n 2 /etc/passwd 顯示最后2行
[root@localhost ~]#tail -q -n k file1 file2 file3 顯示多文件最后k行,并且不顯示文件名的文件頭
[root@localhost ~]#tail -n +k /etc/passwd 從開頭第k行處開始輸出空另。
[root@localhost ~]#tail -f /var/log/messages 參數(shù)-f使tail不停地去讀最新的內(nèi)容盆耽,因此有實(shí)時(shí)監(jiān)視的效果,用Ctrl+c來終止
tail -n+10 file.txt | head -1 顯示file.txt的第10行
[root@localhost ~]#cat 1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost ~]#tail 1.txt
11
12
13
14
15
16
17
18
19
20
[root@localhost ~]#tail -3 1.txt
18
19
20
[root@localhost ~]#tail -n 3 1.txt
18
19
20
[root@localhost ~]#tail --lines=3 1.txt
18
19
20
[root@localhost ~]#tail -n +14 1.txt
14
15
16
17
18
19
20
more 命令
- more命令是用來和man命令查看幫助用
- 空格或者f是下一頁
- 按Space鍵:顯示文本的下一屏內(nèi)容扼菠。 按Enier鍵:只顯示文本的下一行內(nèi)容摄杂。
- 按斜線符|:接著輸入一個(gè)模式,可以在文本中尋找下一個(gè)相匹配的模式循榆。
- 按H鍵:顯示幫助屏析恢,該屏上有相關(guān)的幫助信息。 按B鍵:顯示上一屏內(nèi)容秧饮。
- 按Q鍵:退出rnore命令映挂。
less命令
less命令文件內(nèi)容查看 less命令的作用與more十分相似,都可以用來瀏覽文字檔案的內(nèi)容盗尸,不同的是less命令允許用戶向前或向后瀏覽文件柑船,而more命令只能向前瀏覽。用less命令顯示文件時(shí)泼各,用PageUp鍵向上翻頁鞍时,用PageDown鍵向下翻頁。要退出less程序扣蜻,應(yīng)按Q鍵逆巍。
-e:文件內(nèi)容顯示完畢后,自動(dòng)退出
-f:強(qiáng)制顯示文件
-g:不加亮顯示搜索到的所有關(guān)鍵詞莽使,僅顯示當(dāng)前顯示的關(guān)鍵字锐极,以提高顯示速度;
-l:搜索時(shí)忽略大小寫的差異
-N:每一行行首顯示行號(hào)
-s:將連續(xù)多個(gè)空行壓縮成一行顯示
-S:在單行顯示較長的內(nèi)容芳肌,而不換行顯示溪烤;
-x<數(shù)字>:將TAB字符顯示為指定個(gè)數(shù)的空格字符
tr轉(zhuǎn)換命令
tr命令可以對(duì)來自標(biāo)準(zhǔn)輸入的字符進(jìn)行替換味咳、壓縮和刪除。它可以將一組字符變成另一組字符檬嘀,經(jīng)常用來編寫優(yōu)美的單行命令槽驶,作用很強(qiáng)大。
用法:tr [選項(xiàng)]... SET1 [SET2]
從標(biāo)準(zhǔn)輸入中替換鸳兽、縮減和/或刪除字符掂铐,并將結(jié)果寫到標(biāo)準(zhǔn)輸出。
-c, -C, --complement 首先補(bǔ)足SET1
-d, --delete 刪除匹配SET1 的內(nèi)容揍异,并不作替換
-s, --squeeze-repeats 如果匹配于SET1 的字符在輸入序列中存在連續(xù)的
重復(fù)全陨,在替換時(shí)會(huì)被統(tǒng)一縮為一個(gè)字符的長度
-t, --truncate-set1 先將SET1 的長度截為和SET2 相等
字符 | 介紹 |
---|---|
\\ |
反斜杠 |
\a | 終端鳴響 |
\b | 退格 |
\f | 換頁 |
\n | 換行 |
\r | 回車 |
\t | 水平制表符 |
\v | 垂直制表符 |
[root@localhost ~]#echo "TANK" |tr A-Z a-z 大寫字母轉(zhuǎn)小寫
tank
[root@localhost ~]#echo 'tank zhang' | tr a-z A-Z 小寫字線轉(zhuǎn)大寫
TANK ZHANG
[root@localhost ~]#cat aaa.txt 原文件
aaa
bbb
[root@localhost ~]#cat aaa.txt|tr 'a' 'c' 字母c替換字母a
ccc
bbb
[root@localhost ~]#cat aaa.txt|tr -d 'a' 刪除所有字母a
bbb
[root@localhost ~]#cat aaa.txt|tr -d '\n\t'
aaabbb
刪除文件file中出現(xiàn)的換行'\n'、制表'\t'字符
[root@localhost ~]#cat aaa.txt|tr -s [a-zA-Z] 刪除重復(fù)的字母
a
b
[root@localhost ~]#cat aaa.txt|tr -s '\n' 刪除空行
aaa
bbb
[root@localhost ~]#cat aaa.txt |tr -s '\011' '\040' 用空格符\040替換制表符\011
aaa
bbb
[root@localhost ~]#tr a c < test 將test文件中的a變成c
字符 | 所有和指定字符相等的字符 |
---|---|
[:alnum:] | 所有的字母和數(shù)字 |
[:alpha:] | 所有的字母 |
[:blank:] | 所有呈水平排列的空白字符 |
[:cntrl:] | 所有的控制字符 |
[:digit:] | 所有的數(shù)字 |
[:graph:] | 所有的可打印字符衷掷,包括空格 |
[:lower:] | 所有的小寫字母 |
[:print:] | 所有的可打印字符辱姨,包括空格 |
[:space:] | 所有呈水平或垂直排列的空白字符 |
[:upper:] | 所有的大寫字母 |
[:xdigit:] | 所有的十六進(jìn)制數(shù) |
cut命令
- 在文件的每一行中提取片斷
-b, --bytes=LIST 輸出 這些 字節(jié)
-c, --characters=LIST 輸出 這些 字符
-d, --delimiter=DELIM 使用 DELIM 取代 TAB 做 字段(field) 分隔符
-f, --fields=LIST 輸出 這些 字段
-s, --only-delimited 不顯示 沒有 分隔符 的 行
[root@localhost ~]#cat /etc/passwd | cut -b 1 |head -5 #輸出文件的第一個(gè)字節(jié)
r
b
d
a
l
[root@localhost ~]#cat /etc/passwd | cut -c 1-4 |head -5 #輸出文件的前四個(gè)字符
root
bin:
daem
adm:
lp:x
[root@localhost ~]#cat /etc/passwd | cut -f1 -d ':' |head -5 #以:分割文件,輸出第一個(gè)字段
root
bin
daemon
adm
lp
[root@localhost ~]#cat a.txt |cut -f1,3 -d $'\t' #1戚嗅,3列
ssss dddd
rrr adfa
[root@localhost ~]#cut -c4 file.txt #將所有行的第四個(gè)字符打印出來雨涛。
x
u
l
[root@localhost ~]#cut -c4,6 file.txt #將每一行的第四個(gè)和第六個(gè)字符打印出來
xo
ui
ln
[root@localhost ~]#cut -c4-7 file.txt #將第四個(gè)到第七個(gè)字符打印出來,注意是閉區(qū)間懦胞。
x or
unix
linu
[root@localhost ~]#cut -c-6 file.txt #將每一行的前六個(gè)字符都打印出來
unix o
is uni
is lin
[root@localhost ~]#cut -c10- file.txt #將從起始位置到行末的所有文本都打印出來
inux os
ood os
good os
[root@localhost ~]#cut -d ' ' -f2 file.txt #定義空格為一行的分隔符替久,并將每一行的第二個(gè)字段打印出來
or
unix
linux
[root@localhost ~]#cut -d ' ' -f2,3 file.txt #將第二個(gè)字段和第三個(gè)字段打印出來
or linux
unix good
linux good
[root@localhost ~]#cut -d ' ' -f1-3 file.txt #將第一個(gè)字段、第二個(gè)字段躏尉、第三個(gè)字段的內(nèi)容都打印出來
[root@localhost ~]#cut -d ' ' -f-3 file.txt #將前三個(gè)字段都打印出來
paste命令
- 用法:paste [選項(xiàng)]... [文件]...
-d, --delimiters=列表 改用指定列表里的字符替代制表分隔符
-s, --serial 不使用平行的行目輸出模式蚯根,而是每個(gè)文件占用一行
[root@localhost ~]#paste test1 test 合并輸出二文件
asdfasdfas 1234
asdfasdf
[root@localhost ~]#echo -n "aaa" | paste -s 對(duì)輸出的內(nèi)容獨(dú)立占一行
aaa
wc命令
- wc命令的功能為統(tǒng)計(jì)指定文件中的字節(jié)數(shù)、單詞數(shù)胀糜、行數(shù), 并將統(tǒng)計(jì)結(jié)果顯示輸出
-c, --bytes 印字節(jié)數(shù)
-m, --chars 打印字符數(shù)
-l, --lines 打印行數(shù)
-L, --max-line-length 打印最長行的長度
-w, --words 打印單詞數(shù)
[root@localhost ~]#cat /etc/passwd |wc -l 查看passwd文件有多少行
38
[root@localhost ~]#echo "aaa bbb ccc" |wc -w 查看輸出有多少個(gè)單詞
3
[root@localhost ~]#echo "12344" |wc -m 查看輸出有多少個(gè)字符
6
sort命令
- 用法:sort [選項(xiàng)]... [文件]...
-b, --ignore-leading-blanks 略前導(dǎo)的空白區(qū)域
-d, --dictionary-order 只考慮空白區(qū)域和字母字符
-f, --ignore-case 忽略字母大小寫
-g, --general-numeric-sort 按照常規(guī)數(shù)值排序
-i, --ignore-nonprinting 只排序可打印字符
-h, --human-numeric-sort 使用易讀性數(shù)字(例如: 2K 1G)
-n, --numeric-sort 根據(jù)字符串?dāng)?shù)值比較
-R, --random-sort 根據(jù)隨機(jī)hash 排序
--random-source=文件 從指定文件中獲得隨機(jī)字節(jié)
-r, --reverse 逆序輸出排序結(jié)果
-V, --version-sort 在文本內(nèi)進(jìn)行自然版本排序
-r 執(zhí)行反方向(由上至下)整理
-n 執(zhí)行按數(shù)字大小整理
-t c 選項(xiàng)使用c 做為字段界定符
-k X 選項(xiàng)按照使用c 字符分隔的X
[root@localhost ~]#cat /etc/passwd | sort
sort 是默認(rèn)以第一個(gè)數(shù)據(jù)來排序颅拦,而且默認(rèn)是以字符串形式來排序,所以由字母 a 開始升序排序。
[root@localhost ~]#cat /etc/passwd | sort -t ':' -k 3
/etc/passwd 內(nèi)容是以 : 來分隔的教藻,我想以第三欄來排序矩距,該如何
[root@localhost ~]#cat /etc/passwd | sort -t ':' -k 3n
用數(shù)字排序,默認(rèn)是以字符串來排序的
[root@localhost ~]#cat /etc/passwd | sort -t ':' -k 3nr
倒序排列怖竭,默認(rèn)是升序排序
[root@localhost ~]#cat /etc/passwd | sort -t':' -k 6.2,6.4 -k 1r
對(duì)/etc/passwd,先以第六個(gè)域的第2個(gè)字符到第4個(gè)字符進(jìn)行正向排序,再基于第一個(gè)域進(jìn)行反向排序
[root@localhost ~]#cat /etc/passwd | sort -t':' -k 7 -u
查看/etc/passwd有多少個(gè)shell:對(duì)/etc/passwd的第七個(gè)域進(jìn)行排序陡蝇,然后去重
uniq命令
- 用法:uniq [選項(xiàng)]... [文件]
- 從輸入文件或者標(biāo)準(zhǔn)輸入中篩選相鄰的匹配行并寫入到輸出文件或標(biāo)準(zhǔn)輸出痊臭,不附加任何選項(xiàng)時(shí)匹配行將在首次出現(xiàn)處被合并,相近的行將會(huì)刪除
-c, --count 在每行前加上表示相應(yīng)行目出現(xiàn)次數(shù)的前綴編號(hào)
-d, --repeated 只輸出重復(fù)的行
-D, --all-repeated[=delimit-method 顯示所有重復(fù)的行
-f, --skip-fields=N 比較時(shí)跳過前N 列
-i, --ignore-case 在比較的時(shí)候不區(qū)分大小寫
-s, --skip-chars=N 比較時(shí)跳過前N 個(gè)字符
-u, --unique 只顯示唯一的行
-z, --zero-terminated 使用'\0'作為行結(jié)束符登夫,而不是新?lián)Q行
-w, --check-chars=N 對(duì)每行第N 個(gè)字符以后的內(nèi)容不作對(duì)照
[root@localhost ~]#cat uniqtest 測(cè)試文件
this is a test
this is a test
this is a test
i am tank
i love tank
i love tank
this is a test
whom have a try
WhoM have a try
you have a try
i want to abroad
those are good men
we are good men
[root@localhost ~]#uniq -c uniqtest uniq的一個(gè)特性广匙,檢查重復(fù)行的時(shí)候,只會(huì)檢查相鄰的行恼策。重復(fù)數(shù)據(jù)鸦致,肯定有很多不是相鄰在一起的
3 this is a test
1 i am tank
2 i love tank
1 this is a test 和第一行是重復(fù)的
1 whom have a try
1 WhoM have a try
1 you? have a try
1 i want to abroad
1 those are good men
1 we are good men
[root@localhost ~]#sort uniqtest |uniq -c 這樣就可以解決上個(gè)例子中提到的問題
1 WhoM have a try
1 i am tank
2 i love tank
1 i want to abroad
4 this is a test
1 those are good men
1 we are good men
1 whom have a try
1 you have a try
[root@localhost ~]# uniq -d -c uniqtest uniq -d 只顯示重復(fù)的行
3 this is a test
2 i love tank
[root@localhost ~]# uniq -D uniqtest uniq -D 只顯示重復(fù)的行潮剪,并且把重復(fù)幾行都顯示出來。他不能和-c一起使用
this is a test
this is a test
this is a test
i love tank
i love tank
[root@localhost ~]#uniq -f 1 -c uniqtest 在這里those只有一行分唾,顯示的卻是重復(fù)了抗碰,這是因?yàn)椋?f 1 忽略了第一列,檢查重復(fù)從第二字段開始的绽乔。
3 this is a test
1 i am tank
2 i love tank
1 this is a test
2 whom have a try
1 you have a try
1 i want to abroad
2 those are good men 只有一行弧蝇,顯示二行
[root@localhost ~]#uniq -i -c uniqtest 檢查的時(shí)候,不區(qū)分大小寫
3 this is a test
1 i am tank
2 i love tank
1 this is a test
2 whom have a try #一個(gè)大寫折砸,一個(gè)小寫
1 you have a try
1 i want to abroad
1 those are good men
1 we are good men
[root@localhost ~]#uniq -s 4 -c uniqtest 檢查的時(shí)候看疗,不考慮前4個(gè)字符,這樣whom have a try 就和 you have a try 就一樣了睦授。
3 this is a test
1 i am tank
2 i love tank
1 this is a test
3 whom have a try 根上一個(gè)例子有什么不同
1 i want to abroad
1 those are good men
1 we are good men
[root@localhost ~]#uniq -u uniqtest 去重復(fù)的項(xiàng)两芳,然后全部顯示出來
i am tank
this is a test
whom have a try
WhoM have a try
you have a try
want to abroad
those are good men
we are good men
[root@localhost ~]#uniq -w 2 -c uniqtest 對(duì)每行第2個(gè)字符以后的內(nèi)容不作檢查,所以i am tank 根 i love tank就一樣了去枷。
3 this is a test
3 i am tank
1 this is a test
1 whom have a try
1 WhoM have a try
1 you have a try
1 i want to abroad
1 those are good men
1 we are good men
[root@localhost ~]#grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /var/log/nginx/access.log |sort |uniq -c 查看nginx訪問IP數(shù)
1 101.200.78.64
2 103.41.52.94
1 106.185.47.161
2 113.240.250.155
260 13.0.782.215
2 185.130.5.231
26 192.168.10.16
6 192.168.10.17
148 192.168.10.2
189 192.168.10.202
270 192.168.10.222
25 192.168.10.235
291 192.168.10.3
12 192.168.10.5
2 23.251.63.45
20 7.0.11.0
diff 和 patch命令
- diff比較2個(gè)文件的區(qū)別
[root@localhost ~]#diff test1.rb test.rb 比較二個(gè)文件的不同
[root@localhost ~]#diff myweb/ html/ 比較二個(gè)文件夾的不同
- patch
[root@localhost ~]#diff -Nrua linux-2.6.14/Makefile linux-2.6.26/Makefile >c.patch #cat c.patch
grep命令
- grep的工作方式是這樣的怖辆,它在一個(gè)或多個(gè)文件中搜索字符串模板。如果模板包括空格沉填,則必須被引用疗隶,模板后的所有字符串被看作文件名。搜索的結(jié)果被送到屏幕翼闹,不影響原文件內(nèi)容斑鼻。
- grep 家族分為,三大類分別是grep ,egrep,fgrep猎荠,fgrep不支持正則表達(dá)式
- 我們這里只說grep坚弱,egrep下次在說
--color=auto: 對(duì)匹配到的文本著色顯示
-v: 顯示不被pattern 匹配到的行
-i: 忽略字符大小寫
-n: : 顯示匹配的行號(hào)
-c: 統(tǒng)計(jì)匹配的行數(shù)
-o: 僅顯示匹配到的字符串
-q: 靜默模式,不輸出任何信息
-A #: after, 后#行 行
-B #: before, 前#行 行
-C # :context, 前后各#行 行
-e :實(shí)現(xiàn)多個(gè)選項(xiàng)間的邏輯or 關(guān)系
-w :匹配 整個(gè)單詞
-E :使用ERE
-F :相當(dāng)于fgrep
-E, --extended-regexp 擴(kuò)展正則表達(dá)式egrep
-F, --fixed-strings 一個(gè)換行符分隔的字符串的集合fgrep
-G, --basic-regexp 基本正則
-P, --perl-regexp 調(diào)用的perl正則
-e, --regexp=PATTERN 后面根正則模式关摇,默認(rèn)無
-f, --file=FILE 從文件中獲得匹配模式
-w, --word-regexp 匹配整個(gè)單詞
-x, --line-regexp 匹配整行
-z, --null-data 一個(gè) 0 字節(jié)的數(shù)據(jù)行荒叶,但不是空行
測(cè)試文件 /etc/passwd 里面用到了正則表達(dá)式和擴(kuò)展正則表達(dá)式
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa
DADddd:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
a,匹配含有root的行
[root@localhost ~]#grep root test
root:x:0:0:root:/root:/bin/bash
b,匹配以root開頭或者以zhang開頭的行,注意反斜杠
[root@localhost ~]#cat test |grep '^\(root\|zhang\)'
root:x:0:0:root:/root:/bin/bash
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
c,匹配以root開頭或者以zhang開頭的行输虱,注意反斜杠,根上面一個(gè)例子一樣些楣,-e默認(rèn)是省去的
[root@localhost ~]#cat test |grep -e '^\(root\|zhang\)'
root:x:0:0:root:/root:/bin/bash
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
d,匹配以zhang開頭,只含有字母
[root@localhost ~]#echo 'zhangying' |grep '^zhang[a-z]*$'
zhangying
e,匹配以bin開頭的行,用的egrep宪睹,在這里可以換成-F,-G
[root@localhost ~]#cat test |grep -E '^bin'
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa
f,在匹配的行前面加上該行在文件中愁茁,或者輸出中所在的行號(hào)
[root@localhost ~]#cat test|grep -n zhangy
7:zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
13:ba:x:1002:1002::/home/zhangy:/bin/bash
15:@zhangying:*:1004:1004::/home/test:/bin/bash
g,不匹配以bin開頭的行,并顯示行號(hào)
[root@localhost ~]#cat test|grep -nv '^bin'
root:x:0:0:root:/root:/bin/bash
DADddd:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
h,顯示匹配的個(gè)數(shù),不顯示內(nèi)容
[root@localhost ~]#cat test|grep -c zhang
3
i,匹配system亭病,沒有加-i沒有匹配到東西鹅很。
[root@localhost ~]#grep system test
[root@localhost ~]#grep -ni system test
9:dbus:x:81:81:System message bus:/:/bin/false
j,匹配zhan沒有匹配到東西,匹配zhangy能匹配到罪帖,因?yàn)樵趖est文件中促煮,有zhangy這個(gè)單詞
[root@localhost ~]#cat test|grep -w zhan
[root@localhost ~]#cat test|grep -w zhangy
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
k,在這里-x后面東西邮屁,和輸出中的整行相同時(shí),才會(huì)輸出
[root@localhost ~]#echo "aaaaaa" |grep -x aaa
[root@localhost ~]#echo "aaaa" |grep -x aaaa
aaaa
l,最多只匹配一次菠齿,如果把-m 1去掉的話佑吝,會(huì)有三個(gè)
[root@localhost ~]#cat test |grep -m 1 zhang
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
m,匹配行的前面顯示塊號(hào),這個(gè)塊號(hào)是干什么的泞当,不知道迹蛤,有誰知道可否告訴我一下
[root@localhost ~]#cat test |grep -b zha
241:zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
480:ba:x:1002:1002::/home/zhangy:/bin/bash
558:@zhangying:*:1004:1004::/home/test:/bin/bash
n,多文件匹配時(shí),在匹配的行前面加上文件名
[root@localhost ~]#grep -H 'root' test test2 testbak
test:root:x:0:0:root:/root:/bin/bash
test2:root
testbak:root:x:0:0:root:/root:/bin/bash
o,多文件匹配時(shí)襟士,在匹配的行前面不加上文件名
[root@localhost ~]#grep -h 'root' test test2 testbak
root:x:0:0:root:/root:/bin/bash
root
root:x:0:0:root:/root:/bin/bash
p,多文件匹配時(shí)盗飒,顯示匹配文件的文件名
[root@localhost ~]#grep -l 'root' test test2 testbak DAta
test
test2
testbak
q,沒有-o時(shí),有一行匹配陋桂,這一行里面有3個(gè)root逆趣,加上-o后,這個(gè)3個(gè)root就出來了
[root@localhost ~]#grep 'root' test
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]#grep -o 'root' test
root
root
root
r,遞歸顯示匹配的內(nèi)容嗜历,在test目錄下面建個(gè)mytest目錄宣渗,copy test目錄下面的test文件到mytest下面,能看到上面的結(jié)果
[root@localhost ~]#grep test -R /tmp/test/mytest
/tmp/test/mytest/test:test:x:1003:1003::/home/test:/bin/bash
/tmp/test/mytest/test:@zhangying:*:1004:1004::/home/test:/bin/bash
s,顯示匹配root后面的3行
[root@localhost ~]#cat test |grep -A 3 root
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
遞歸從所有文件中查詢匹配的內(nèi)容梨州,文件名可不同
[root@localhost ~]#grep -R C1079651000621 *
20150727/503/20150701000104001317.xml: C1079651000621
20150727/503/20150701000104001317.xml: C1079651000621
20150727/503/20150701000104001333.xml: C1079651000621
正則表達(dá)式
基本正則表達(dá)式
- 我們先說字符匹配
- 正則表達(dá)式和我們以前所說的文件通配符很相似痕囱, 正則表達(dá)式用來不是來處理文件的名稱
- 他匹配的是文件的內(nèi)容或者是字符串, 不過千萬要搞清楚正則表達(dá)式來處理的是文本文件暴匠,或者是字符串而不是文件名鞍恢,通配符的匹配的是文件名,他是文件名里面的特定字符串每窖,而正則表達(dá)式里面的是匹配的字符串帮掉,這個(gè)字符串可能是文本內(nèi)容里面的字符串,當(dāng)然也可能是命令執(zhí)行結(jié)果里面的字符串窒典。
- -正則表達(dá)式的功能很強(qiáng)也分了若干類別分為蟆炊,字符匹配,匹配次數(shù)瀑志,位置錨定涩搓,分組
- 其有用于字符匹配的,我們叫字符匹配
- 也有匹配某個(gè)字符重復(fù)次數(shù)的劈猪,我們叫匹配次數(shù)
- 也可以用來確實(shí)這個(gè)字符出現(xiàn)的位置的昧甘,我們叫位置錨定
- 也可以把多個(gè)字符用來合成一個(gè)整體,我們叫分組
- 所以正則表達(dá)式的功能非常強(qiáng)
- 正則表達(dá)式用到了一些特殊符號(hào)岸霹,我們叫元字符
- 如果想詳細(xì)的看可以用幫助命令,man 7
- 我們先看字符匹配
字符匹配
. 字符
- 如果我要匹配一個(gè)的單一字符用
.
就要可以了,.
表示單一的一個(gè)字符 -
.
是匹配一個(gè)文件中的內(nèi)容中的一個(gè)單一的字符 - 比如說我要匹配一個(gè)內(nèi)容中的一個(gè)字符
[root@localhost ~]# echo abcd|grep a.
ab
cd
[root@localhost ~]# echo abcd|grep a..
abc
d
[root@localhost ~]# echo abcd|grep a...
abcd
- 我加一個(gè)點(diǎn)就多匹配一個(gè),但是這個(gè)adcd匹配嗎将饺,匹配前給你列出來了贡避,但是他只會(huì)給他痛黎,匹配到的加上顏色,漢字也會(huì)匹配哦刮吧,所以說他匹配的是任意字符湖饱,注意了點(diǎn)是匹配的任意字符,不代表他們匹配的是同一個(gè)字符
[root@localhost ~]# grep r..t /etc/passwd
root
:x:0:0:root
:/root
:/bin/bash
operator:x:11:0:operator:/root
:/sbin/nologin
ftp:x:14:50:FTP User:/var/ft
p:/sbin/nologin
- 看他上面匹配的是任意字符不只匹配
root
,r/ft
都匹配到了杀捻,這就是匹配任意字符 - 同樣點(diǎn)也表示一個(gè)字符
[ ]中括號(hào) 字符
[]
中括號(hào)在正則表達(dá)式中井厌,他也是表示一個(gè)字符,表示中間的任意一個(gè)字符致讥,不過他匹配的是字符串下面這個(gè)命令表示可能是
ar
,cr
,br
的命令
[root@localhost ~]# echo ar br cr |grep [ab]r
ar
br
cr
-
ar
,cr
是匹配的cr
我沒說仅仆,所以不包含
- 這是
[]
中括號(hào)中表示任意的單一的一個(gè)字符,他取的是中間的一個(gè)字符 - 而且還是二選一的垢袱,或者是多選一的
- 當(dāng)然了我沒還可以取反
[^ ] 取反字符
- [^ ] 取反的意思就是匹配指定范圍外的任意單個(gè)字符
- 取反意思就是墓拜,除了
[]
不要,其余的都要的意思 - 也就是只要不是這個(gè)范圍內(nèi)的请契,其他任意字符都行
[root@localhost ~]# echo ar br cr |grep [^ab]r
ar brcr
匹配次數(shù)
* 字符
-
*
的意思是不確定次數(shù),包括0
次 - 注意他重復(fù)的是前面這個(gè)字符任意次咳榜,不是單詞任意次
[root@localhost ~]# grep goo*gle 11.txt
gooooooooooogle
. * 字符
-
.*
的含義是任意長度的字符串
[root@localhost ~]# grep g.*gle 11.txt
gooooooooooogle
gooooogle
goooooooogle
\ ? 字符
- 他的意思是匹配這個(gè)字符串前面的這個(gè)字符出現(xiàn)
1
次或者0
次 - 簡單的說就是0次就是沒有,1次就是有
[root@localhost ~]# grep "go?gle" 11.txt
ggle
gogle
\ + 字符
- 當(dāng)然了我們也可以表示
1
次以上,包含1
次
[root@localhost ~]# grep "go+gle" 11.txt
gooooooooooogle
gooooogle
goooooooogle
gogle
{N} 字符
- 匹配前面N次
- 如果我們想匹配
5
次,或2次
[root@localhost ~]# grep "go{5}gle" 11.txt
gooooogle
[root@localhost ~]# grep "go{2}gle" 11.txt
- 他還可以表示幾個(gè)以上
[root@localhost ~]# grep "go{2,}gle" 11.txt
gooooooooooogle
gooooogle
goooooooogle
- 還可以表示范圍
[root@localhost ~]# grep "go{2,5}gle" 11.txt
gooooogle
還可以表示以下
[root@localhost ~]# grep "go{,5}gle" 11.txt
google
gooooogle
ggle
gogle
以上是匹配次數(shù)
位置錨定
^ 錨定 首行
[root@localhost ~]# grep "^root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
- 在外面的 托字符
^
意思是不以他開頭 - 在里面的托字符 [^ ] 在里面是以他開頭
$ 字符
- $ 是顯示從尾部顯示
[root@localhost ~]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
mageedu:x:1000:1000:mageedu:/home/mageedu:/bin/bash
mage:x:1001:1001::/home/mage:/bin/bash
wang:x:1002:1002::/home/wang:/bin/bash
shadow:x:0:1003::/home/shadow:/bin/bash
hyma:x:0:1004::/home/hyma:/bin/bash
- 也可以和其他來配合使用
[root@localhost ~]# grep ^google$ 11.txt
google
- 當(dāng)然如果
^$
連著寫是表示空行
[root@localhost ~]# grep -n ^$ 11.txt
1:
2:
3:
4:
5:
6:
7:
8:
15:
16:
17:
18:
19:
20:
22:
31:
- 還可以用他來排除空行
[root@localhost ~]# grep -nv ^$ 11.txt
9:google
10:gooooooooooogle
11:gooooogle
12:goooooooogle
13:ggle
14:gogle
21: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
23:lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
24: inet 127.0.0.1 netmask 255.0.0.0
25: inet6 ::1 prefixlen 128 scopeid 0x10<host>
26: loop txqueuelen 1 (Local Loopback)
27: RX packets 6 bytes 446 (446.0 B)
28: RX errors 0 dropped 0 overruns 0 frame 0
29: TX packets 6 bytes 446 (446.0 B)
30: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- 單詞錨定
[root@localhost ~]# grep "^\<lilin\>" /etc/passwd
lilin:x:1003:1005::/home/lilin:/bin/bash
- 開頭單詞錨定
[root@localhost ~]# grep "^\<lilin" /etc/passwd
lilin:x:1003:1005::/home/lilin:/bin/bash
- 單詞的左右邊界
[root@localhost ~]# grep "^\<root\>" /etc/passwd
root:x:0:0:root:/root:/bin/bash
- 也可以用\b來表示邊界
[root@localhost ~]# grep "^\broot\b" /etc/passwd
root:x:0:0:root:/root:/bin/bash
分組
- 他是把多個(gè)字符串爽锥,作為一個(gè)整體來用
[root@localhost ~]# echo rererererre |grep "\(re\)\{3\}"
rererererre
字符 | 說明 |
---|---|
. | 匹配任意單個(gè)字符 |
[ ] | 匹配指定范圍內(nèi)的任意單個(gè)字符 |
[^ ] | 匹配指定范圍外的任意單個(gè)字符 |
[:alnum:] | 字母和 數(shù)字 |
[:alpha:] | 代表任何英文大小寫字符涌韩,亦即 A-Z, a-z |
[:lower:] | 小寫字母 [:upper:] 大寫字母 |
[:blank:] | 空白字符(空格和制表符) |
[:space:] | 水平和垂直的空白字符(比[:blank:] 包含的范圍廣) |
[:cntrl:] | 不可打印的控制字符(退格、刪除氯夷、警鈴...) ) |
[:digit:] | 字 十進(jìn)制數(shù)字 [:xdigit:] 十六進(jìn)制數(shù)字 |
[:graph:] | 可打印的非空白字符 |
[:print:] | 可打印字符 |
- 以上是linux基本正則表達(dá)式元字符
- 正則表達(dá)式分為兩大類分別是BRE基本的正則表達(dá)式臣樱,ERE 擴(kuò)展的正則表達(dá)式
|BRE元符| 說明 | 舉例 |
| :-------- | --------:|| :-------- |
|. | 匹配單個(gè)字符 | a.c |
|[ ] | 匹配范圍內(nèi)任意單個(gè)字符 | [a-z]|
|[ ] | [[:digit:]]:匹配0-9之間的數(shù)字| 1[[:digit:]]|
|[ ] | [[:alpha:]]:匹配任意字母字符,不區(qū)分大小寫| a[[:alpha:]]|
|[ ] | [[:alnum:]]:匹配任意字母數(shù)字字符0-9肠槽,a-z和 A-Z | a[[:alnum:]]789|
|[ ] | [[:blnk:]]:匹配空格或制表符 |Hello[[:blank:]]world |
|[ ] | [[:lower:]]:匹配小寫字母a-z |abcde[[:lower:]]g |
|[ ] | [[:upper:]]:匹配小寫字母A-Z|ABCDEF[[:upper:]]G |
|[ ] | [[:prit:]]:匹配任意可打印字符 | |
|[ ] | [[:punct:]]:匹配標(biāo)點(diǎn)符號(hào)|attention[[:punct:]] |
|[ ] | [[:space:]]]:匹配任意空白字 | Hello[[:blank:]]world |
|[^ ] | 匹配范圍外任意單個(gè)字符 | [^a-z]|
|* | 匹配求按摩的字符任意次(0,1或多次) | ab |
|. | .任意長度的任意字符 | . 整行 |
|+ | 匹配前面的字符至少1次| a+b |
|? | 匹配前面的0或1次 |a?b |
|{m} | 其前面的字符出現(xiàn)m次擎淤,m為非負(fù)整數(shù) | a{2.4} |
|{m,n} | 其前面的字符出現(xiàn)m次,m為非負(fù)整數(shù)|b{2,4} |
|^ | 行首匹配 | ^Head |
| $
| 行位匹配 | tail$ |
| <或>\b | 匹配單詞左側(cè) | \Hello |
|>或\b | 匹配單詞右側(cè) | hello> |
|(x) | 將此x匹配到的字符當(dāng)做整體進(jìn)行處理| |
|(x) | pat1(pat2)pat3(pat4(pat5)pat6) | \2 引用is |
|(x) | \n:第n個(gè)括號(hào)的匹配模式所匹配到的內(nèi)容 | |
- 以上是正則表達(dá)式BRE的元字符
-以下是正則表達(dá)式ERE的元字符
|ERE元符| 說明 | 舉例 |
| :-------- | --------:|| :-------- |
|. | 匹配單個(gè)字符 | a.c |
| [ ] | 匹配范圍內(nèi)任意單個(gè)字符 | [a-z]|
|[ ] | [[:digit:]]:匹配0-9之間的數(shù)字| 1[[:digit:]]|
|[ ] | [[:alpha:]]:匹配任意字母字符秸仙,不區(qū)分大小寫| a[[:alpha:]]|
|[ ] | [[:alnum:]]:匹配任意字母數(shù)字字符0-9嘴拢,a-z和 A-Z | a[[:alnum:]]789|
|[ ] | [[:blnk:]]:匹配空格或制表符 |Hello[[:blank:]]world |
|[ ] | [[:lower:]]:匹配小寫字母a-z |abcde[[:lower:]]g |
|[ ] | [[:upper:]]:匹配小寫字母A-Z|ABCDEF[[:upper:]]G |
|[ ] | [[:prit:]]:匹配任意可打印字符 | |
|[ ] | [[:punct:]]:匹配標(biāo)點(diǎn)符號(hào)|attention[[:punct:]] |
|[ ] | [[:space:]]]:匹配任意空白字 | Hello[[:blank:]]world |
|[^ ] | 匹配范圍外任意單個(gè)字符 | [^a-z]|
|* | 匹配求按摩的字符任意次(0,1或多次) | ab |
|. | .任意長度的任意字符 | . 整行 |
|?| 匹配前面的0次或1次 | a?b |
|{m} | 其前面的字符出現(xiàn)m次寂纪,m為非負(fù)整數(shù) | a{4}|
|{m,n} | 其前面的字符出現(xiàn)m次席吴,m為非負(fù)整數(shù);[m,n] | b{2.4}|
|^ | 行首匹配 | ^Head |
| $
| 行位匹配 | tail$ |
| <或\b| 匹配單詞左側(cè)| <hello> |
| >或\b | 匹配單詞右側(cè)| hello> |
|(x) | 將此x匹配到的字符當(dāng)做整體進(jìn)行處理| |
|(x) | pat1(pat2)pat3(pat4(pat5)pat6) | \2 引用is |
|(x) | \n:第n個(gè)括號(hào)的匹配模式所匹配到的內(nèi)容 |
- 還有最后一個(gè)| 線由于編程的沖突不能填寫進(jìn)表格里面他的作用是,匹配左邊或右邊