以下內容轉載自http://hi.baidu.com/jackywdx/blog/item/d831471fedd6d1cda78669c5.html
Grep : g (globally) search for a re (regular e-xpression ) and p (print ) the results.
1、參數:
-I :忽略大小寫
-c :列印匹配的行數
-l :從多個檔案中尋找包含匹配項
-v :尋找不包含匹配項的行
-n:列印包含匹配項的行和行標
2、RE(Regex)
\ 忽略Regex中特殊字元的原有含義
^ 匹配Regex的開始行
$ 匹配Regex的結束行
\< 從匹配Regex的行開始
\> 到匹配Regex的行結束
[ ] 單個字元;如[A] 即A符合要求
[ - ] 範圍 ;如[A-Z]即A,B,C一直到Z都符合要求
. 所有的單個字元
* 所有字元,長度可以為0
3、舉例
# ps -ef | grep in.telnetd
root 19955 181 0 13:43:53 ? 0:00 in.telnetd
# more size.txt size檔案的內容
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# more size.txt | grep '[a-b]' 範圍 ;如[A-Z]即A,B,C一直到Z都符合要求
b124230
b034325
a081016
a022021
a061048
b103303
a013386
b044525
# more size.txt | grep '[a-b]'*
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# more size.txt | grep '[b]' 單個字元;如[A] 即A符合要求
b124230
b034325
b103303
b044525
# more size.txt | grep '[bB]'
b124230
b034325
b103303
b044525
B081016
B103303
BADc2345
# grep 'root' /etc/group
root::0:root
bin::2:root,bin,daemon
sys::3:root,bin,sys,adm
adm::4:root,adm,daemon
uucp::5:root,uucp
mail::6:root
tty::7:root,tty,adm
lp::8:root,lp,adm
nuucp::9:root,nuucp
daemon::12:root,daemon
# grep '^root' /etc/group 匹配Regex的開始行
root::0:root
# grep 'uucp' /etc/group
uucp::5:root,uucp
nuucp::9:root,nuucp
# grep '\<uucp' /etc/group
uucp::5:root,uucp
# grep 'root$' /etc/group 匹配Regex的結束行
root::0:root
mail::6:root
# more size.txt | grep -i 'b1..*3' -i :忽略大小寫
b124230
b103303
B103303
# more size.txt | grep -iv 'b1..*3' -v :尋找不包含匹配項的行
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
a013386
b044525
m8987131
B081016
M45678
BADc2345
# more size.txt | grep -in 'b1..*3'
1:b124230
9:b103303
15:B103303
# grep '$' /etc/init.d/nfs.server | wc -l
128
# grep '\$' /etc/init.d/nfs.server | wc –l 忽略Regex中特殊字元的原有含義
15
# grep '\$' /etc/init.d/nfs.server
case "$1" in
>/tmp/sharetab.$$
[ "x$fstype" != xnfs ] &&
echo "$path\t$res\t$fstype\t$opts\t$desc"
>>/tmp/sharetab.$$
/usr/bin/touch -r /etc/dfs/sharetab /tmp/sharetab.$$
/usr/bin/mv -f /tmp/sharetab.$$ /etc/dfs/sharetab
if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[ ]*(#|$)'
if [ $startnfsd -eq 0 -a -f /etc/rmmount.conf ] &&
if [ $startnfsd -ne 0 ]; then
elif [ ! -n "$_INIT_RUN_LEVEL" ]; then
while [ $wtime -gt 0 ]; do
wtime=`expr $wtime - 1`
if [ $wtime -eq 0 ]; then
echo "Usage: $0 { start | stop }"
# more size.txt
the test file
their are files
The end
# grep 'the' size.txt
the test file
their are files
# grep '\<the' size.txt
the test file
their are files
# grep 'the\>' size.txt
the test file
# grep '\<the\>' size.txt
the test file
# grep '\<[Tt]he\>' size.txt
the test file
==================================================================
1,簡介
使用Regex的一個多用途文本搜尋工具.這個命令本來是ed行編輯器中的一個命令/過濾器:
g/re/p -- global - regular expression - print.
基本格式
grep pattern
(1)grep 搜尋字串 [filename]
(2)grep Regex [filename]
在檔案中搜尋所有 pattern 出現的位置, pattern 既可以是要搜尋的字串,也可以是一個Regex.
注意:在輸入要搜尋的字串時最好使用雙引號/而在模式比對使用Regex時,注意使用單引號
2,grep的選項
-c 只輸出匹配行的計數
-i 不區分大小寫(用於單字元)
-n 顯示匹配的行號
-v 不顯示不包含匹配文本的所以有行
-s 不顯示錯誤資訊
-E 使用擴充Regex
更多的選項請查看:man grep
3,常用grep執行個體
(1)多個檔案查詢
grep "sort" *.doc #見檔案名稱的匹配
(2)行匹配:輸出匹配行的計數
grep -c "48" data.doc #輸出文檔中含有48字元的行數
(3)顯示匹配行和行數
grep -n "48" data.doc #顯示所有匹配48的行和行號
(4)顯示非匹配的行
grep -vn "48" data.doc #輸出所有不包含48的行
(4)顯示非匹配的行
grep -vn "48" data.doc #輸出所有不包含48的行
(5)大小寫敏感
grep -i "ab" data.doc #輸出所有含有ab或Ab的字串的行
4, Regex的應用
(1)Regex的應用 (注意:最好把Regex用單引號括起來)
grep '[239].' data.doc #輸出所有含有以2,3或9開頭的,並且是兩個數位行
(2)不匹配測試
grep '^[^48]' data.doc #不匹配行首是48的行
(3)使用擴充模式比對
grep -E '219|216' data.doc
(4) ...
這需要在實踐中不斷應用和總結,熟練掌握Regex。
5, 使用類名
可以使用國際模式比對的類名:
[[:upper:]] [A-Z]
[[:lower:]] [a-z]
[[:digit:]] [0-9]
[[:alnum:]] [0-9a-zA-Z]
[[:space:]] 空格或tab
[[:alpha:]] [a-zA-Z]
(1)使用
grep '5[[:upper:]][[:upper:]]' data.doc #查詢以5開頭以兩個大寫字母結尾的行