4.grep命令使用簡單實例 $ grep ‘test’ d* 顯示所有以d開頭的文件中包含
test的行。 $ grep ‘test’ aa bb cc 顯示在aa,bb,cc文件中匹配test的行。 $ grep
‘[a-z]\{5\}’ aa 顯示所有包含每個字符串至少有5個連續小寫字符的字符串的行。 $ grep ‘w\(es\)t.*\1′
aa 如果west被匹配,則es就被存儲到內存中,并標記為1,然后搜索任意個字符(.*),這些字符后面緊跟著
另外一個es(\1),找到就顯示該行。如果用egrep或grep -E,就不用”\”號進行轉義,直接寫成’w(es)t.*\1′就可以了。
5.grep命令使用復雜實例 假設您正在’/usr/src/Linux/Doc’目錄下搜索帶字符
串’magic’的文件: $ grep magic /usr/src/Linux/Doc/* sysrq.txt:* How do I enable
the magic SysRQ key? sysrq.txt:* How do I use the magic SysRQ
key? 其中文件’sysrp.txt’包含該字符串,討論的是 SysRQ 的功能。 默認情況下,’grep’只搜索當前目錄。如果
此目錄下有許多子目錄,’grep’會以如下形式列出: grep: sound: Is a directory 這可能會使’grep’
的輸出難于閱讀。這里有兩種解決的辦法: 明確要求搜索子目錄:grep -r 或忽略子目錄:grep -d skip 如果有很多
輸出時,您可以通過管道將其轉到’less’上閱讀: $ grep magic /usr/src/Linux/Documentation/* |
less 這樣,您就可以更方便地閱讀。
可以用^符號做[]內的前綴,表示除[]內的字符之外的字 符。 比如搜索oo前沒有g的字符串所在的行. 使用 '[^g]oo' 作搜索字符串 woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt 2:apple is my favorite food. 3:Football game is not use feet only. 18:google is the best tools for search keyword. 19:goooooogle yes!
[] 內可以用范圍表示,比如[a-z] 表示小寫字母,[0-9] 表示0~9的數字, [A-Z] 則是大寫字母們。[a-zA-Z0-9]表示所有數字與英文字符。 當然也可以配合^來排除字符。 搜索包含數字的行 woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt 5:However ,this dress is about $ 3183 dollars. 15:You are the best is menu you are the no.1.
行首與行尾字符 ^ $. ^ 表示行的開頭,$表示行的結尾( 不是字符,是位置)那么‘^$' 就表示空行,因為只有 行首和行尾。 這里^與[]里面使用的^意義不同。它表示^后面的串是在行的開頭。 比如搜索the在開頭的行 woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt 12:the symbol '*' is represented as star.
搜索以小寫字母開頭的行 woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt 2:apple is my favorite food. 4:this dress doesn't fit me. 10:motorcycle is cheap than car. 12:the symbol '*' is represented as star. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go. woody@xiaoc:~/tmp$
搜索開頭不是英文字母的行 woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 21:#I am VBird woody@xiaoc:~/tmp$
$表示它前面的串是在行的結尾,比如 '\.' 表示 . 在一行的結尾 搜索末尾是.的行 woody@xiaoc:~/tmp$ grep -n '\.$' regular_express.txt //. 是正則表達式的特殊符號,所以要用\轉義 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 4:this dress doesn't fit me. 5:However ,this dress is about $ 3183 dollars. 6:GNU is free air not free beer. .....
搜索非空行 woody@xiaoc:~/tmp$ grep -vn '^$' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 4:this dress doesn't fit me. ..........
點. 代表一個任意字符,必須存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。
woody@xiaoc:~/tmp$ grep -n 'g..d' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 9:Oh! the soup taste good! 16:The world is the same with 'glad'. woody@xiaoc:~/tmp$
搜索兩個o以上的字符串 woody@xiaoc:~/tmp$ grep -n 'ooo*' regular_express.txt //前兩個o一定存在,第三個o可沒有,也可有多個。 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! the soup taste good! 18:google is the best tools for search keyword. 19:goooooogle yes!
搜索g開頭和結尾,中間是至少一個o的字符串,即gog, goog....gooog...等 woody@xiaoc:~/tmp$ grep -n 'goo*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
搜索g開頭和結尾的字符串在的行 woody@xiaoc:~/tmp$ grep -n 'g.*g' regular_express.txt // .*表示 0個或多個任意字符 1:"Open Source" is a good mechanism to develop programs. 14:The gd software is a library for drafting programs. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go.
搜索包含兩個o的字符串的行。 woody@xiaoc:~/tmp$ grep -n 'o\{2\}' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! the soup taste good! 18:google is the best tools for search keyword. 19:goooooogle yes!
搜索g后面跟2~5個o,后面再跟一個g的字符串的行。 woody@xiaoc:~/tmp$ grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword.
搜索包含g后面跟2個以上o,后面再跟g的行。。 woody@xiaoc:~/tmp$ grep -n 'go\{2,\}g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
擴展正則表達式是對基礎正則表達式添加了幾個特殊構成的。 它令某些操作更加方便。 比如我們要去除 空白行和行首為 #的行, 會這樣用: woody@xiaoc:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#' "Open Source" is a good mechanism to develop programs. apple is my favorite food. Football game is not use feet only. this dress doesn't fit me. ............
然而使用支持擴展正則表達式的 egrep 與擴展特殊符號 | ,會方便許多。 注意grep只支持基礎表達式, 而egrep 支持擴展的, 其實 egrep 是 grep -E 的別名而已。因此grep -E 支持擴展正則。 那么: woody@xiaoc:~/tmp$ egrep -v '^$|^#' regular_express.txt "Open Source" is a good mechanism to develop programs. apple is my favorite food. Football game is not use feet only. this dress doesn't fit me. .................... 這里| 表示或的關系。 即滿足 ^$ 或者 ^# 的字符串。
2014-12-02
-a :將 binary 文件以 text 文件的方式搜尋數據
-c :計算找到 '搜尋字符串' 的次數
-i :忽略大小寫的不同,所以大小寫視為相同
-n :順便輸出行號
-v :反向選擇,顯示出沒有 '搜索字符串' 內容的那一行!
2014-12-02
默認情況下,‘grep’只搜索當前目錄。如果此目錄下有許多子目錄,‘grep’會以如下形式列出
例如
grep: sound: Is a directory
這可能會使‘grep’的輸出難于閱讀。這里有兩種解決的辦法:
明確要求搜索子目錄:grep -r
或忽略子目錄:grep -d skip
當然,如果預料到有許多輸出,您可以通過 管道 將其轉到‘less’上閱讀
例如:
$ grep magic /usr/src/linux/Documentation/* | less
2014-12-02
-c???打印出匹配行的總數,而不是打印出匹配的行
-E???打開擴展表達式
-h???禁止將在其中查找到匹配內容的文件名作為輸出行的前綴
2014-12-02
grep -i pattern files :不區分大小寫地搜索。默認情況區分大小寫,
grep -l pattern files :只列出匹配的文件名,
grep -L pattern files :列出不匹配的文件名,
grep -w pattern files :只匹配整個單詞,而不是字符串的一部分(如匹配‘magic’,而不是‘magical’),
grep -C number pattern files :匹配的上下文分別顯示[number]行,
grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行,
grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。
這里還有些用于搜索的特殊符號:
\< 和 \> 分別標注單詞的開始與結尾。
例如:
grep man * 會匹配 ‘Batman’、‘manic’、‘man’等,
grep '\<man' * 匹配‘manic’和‘man’,但不是‘Batman’,
grep '\<man\>' 只匹配‘man’,而不是‘Batman’或‘manic’等其他的字符串。
'^':指匹配的字符串在行首,
'$':指匹配的字符串在行尾,
2014-12-02
linux grep命令
1.作用
Linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹 配的行打印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。
2.格式
grep [options]
3.主要參數
[options]主要參數:
-c:只輸出匹配行的計數。
-I:不區分大 小寫(只適用于單字符)。
-h:查詢多文件時不顯示文件名。
-l:查詢多文件時只輸出包含匹配字符的文件名。
-n:顯示匹配行及 行號。
-s:不顯示不存在或無匹配文本的錯誤信息。
-v:顯示不包含匹配文本的所有行。
pattern正則表達式主要參數:
\: 忽略正則表達式中特殊字符的原有含義。
^:匹配正則表達式的開始行。
$: 匹配正則表達式的結束行。
\<:從匹配正則表達 式的行開始。
\>:到匹配正則表達式的行結束。
[ ]:單個字符,如[A]即A符合要求 。
[ - ]:范圍,如[A-Z],即A、B、C一直到Z都符合要求 。
。:所有的單個字符。
* :有字符,長度可以為0。
4.grep命令使用簡單實例
$ grep ‘test’ d*
顯示所有以d開頭的文件中包含 test的行。
$ grep ‘test’ aa bb cc
顯示在aa,bb,cc文件中匹配test的行。
$ grep ‘[a-z]\{5\}’ aa
顯示所有包含每個字符串至少有5個連續小寫字符的字符串的行。
$ grep ‘w\(es\)t.*\1′ aa
如果west被匹配,則es就被存儲到內存中,并標記為1,然后搜索任意個字符(.*),這些字符后面緊跟著 另外一個es(\1),找到就顯示該行。如果用egrep或grep -E,就不用”\”號進行轉義,直接寫成’w(es)t.*\1′就可以了。
5.grep命令使用復雜實例
假設您正在’/usr/src/Linux/Doc’目錄下搜索帶字符 串’magic’的文件:
$ grep magic /usr/src/Linux/Doc/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?
其中文件’sysrp.txt’包含該字符串,討論的是 SysRQ 的功能。
默認情況下,’grep’只搜索當前目錄。如果 此目錄下有許多子目錄,’grep’會以如下形式列出:
grep: sound: Is a directory
這可能會使’grep’ 的輸出難于閱讀。這里有兩種解決的辦法:
明確要求搜索子目錄:grep -r
或忽略子目錄:grep -d skip
如果有很多 輸出時,您可以通過管道將其轉到’less’上閱讀:
$ grep magic /usr/src/Linux/Documentation/* | less
這樣,您就可以更方便地閱讀。
有一點要注意,您必需提供一個文件過濾方式(搜索全部文件的話用 *)。如果您忘了,’grep’會一直等著,直到該程序被中斷。如果您遇到了這樣的情況,按 <CTRL c> ,然后再試。
下面還有一些有意思的命令行參數:
grep -i pattern files :不區分大小寫地搜索。默認情況區分大小寫,
grep -l pattern files :只列出匹配的文件名,
grep -L pattern files :列出不匹配的文件名,
grep -w pattern files :只匹配整個單詞,而不是字符串的一部分(如匹配’magic’,而不是’magical’),
grep -C number pattern files :匹配的上下文分別顯示[number]行,
grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行,
grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。
grep -n pattern files? 即可顯示行號信息
grep -c pattern files? 即可查找總行數
這里還有些用于搜索的特殊符號:
\< 和 \> 分別標注單詞的開始與結尾。
例如:
grep man * 會匹配 ‘Batman’、’manic’、’man’等,
grep ‘\<man’ * 匹配’manic’和’man’,但不是’Batman’,
grep ‘\<man\>’ 只匹配’man’,而不是’Batman’或’manic’等其他的字符串。
‘^’:指匹配的字符串在行首,
‘$’:指匹配的字符串在行 尾,
Grep 命令 用法大全
1、 參數:
-I :忽略大小寫
-c :打印匹配的行數
-l :從多個文件中查找包含匹配項
-v :查找不包含匹配項的行
-n:打印包含匹配項的行和行標
2、RE(正則表達式)
\ 忽略正則表達式中特殊字符的原有含義
^ 匹配正則表達式的開始行
$ 匹配正則表達式的結束行
\< 從匹配正則表達式的行開始
\> 到匹配正則表達式的行結束
[ ] 單個字符;如[A] 即A符合要求
[ - ] 范圍 ;如[A-Z]即A,B,C一直到Z都符合要求
. 所有的單個字符
* 所有字符,長度可以為0
2014-12-02
參考 http://www.cnblogs.com/end/archive/2012/02/21/2360965.html
Linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹 配的行打印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。
-c:只輸出匹配行的計數。
-I:不區分大 小寫(只適用于單字符)。
-h:查詢多文件時不顯示文件名。
-l:查詢多文件時只輸出包含匹配字符的文件名。
-n:顯示匹配行及 行號。
-s:不顯示不存在或無匹配文本的錯誤信息。
-v:顯示不包含匹配文本的所有行。
pattern正則表達式主要參數:
\: 忽略正則表達式中特殊字符的原有含義。
^:匹配正則表達式的開始行。
$: 匹配正則表達式的結束行。
\<:從匹配正則表達 式的行開始。
\>:到匹配正則表達式的行結束。
[ ]:單個字符,如[A]即A符合要求 。
[ - ]:范圍,如[A-Z],即A、B、C一直到Z都符合要求 。
。:所有的單個字符。
* :有字符,長度可以為0。
4.grep命令使用簡單實例
$ grep ‘test’ d*
顯示所有以d開頭的文件中包含 test的行。
$ grep ‘test’ aa bb cc
顯示在aa,bb,cc文件中匹配test的行。
$ grep ‘[a-z]\{5\}’ aa
顯示所有包含每個字符串至少有5個連續小寫字符的字符串的行。
$ grep ‘w\(es\)t.*\1′ aa
如果west被匹配,則es就被存儲到內存中,并標記為1,然后搜索任意個字符(.*),這些字符后面緊跟著 另外一個es(\1),找到就顯示該行。如果用egrep或grep -E,就不用”\”號進行轉義,直接寫成’w(es)t.*\1′就可以了。
2014-12-02
在linux中grep命令是非常有用的,它和管道(|)配合使用,非常強大,用于搜索文本文件.如果想要在幾個文本文件中查找一字符串,可以使用‘grep’命令?!甮rep’在文本中搜索指定的字符串。
假設您正在‘/usr/src/linux/Documentation’目錄下搜索帶字符串‘magic’的文件:
$ grep magic /usr/src/linux/Documentation/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?
其中文件‘sysrp.txt’包含該字符串,討論的是 SysRQ 的功能。
默認情況下,‘grep’只搜索當前目錄。如果此目錄下有許多子目錄,‘grep’會以如下形式列出:
grep: sound: Is a directory
這可能會使‘grep’的輸出難于閱讀。這里有兩種解決的辦法:
明確要求搜索子目錄:grep -r
或忽略子目錄:grep -d skip
當然,如果預料到有許多輸出,您可以通過 管道 將其轉到‘less’上閱讀:
$ grep magic /usr/src/linux/Documentation/* | less
這樣,您就可以更方便地閱讀。
有一點要注意,您必需提供一個文件過濾方式(搜索全部文件的話用 *)。如果您忘了,‘grep’會一直等著,直到該程序被中斷。如果您遇到了這樣的情況,按 <CTRL c> ,然后再試。
下面是一些有意思的命令行參數:
grep -i pattern files :不區分大小寫地搜索。默認情況區分大小寫,
grep -l pattern files :只列出匹配的文件名,
grep -L pattern files :列出不匹配的文件名,
grep -w pattern files :只匹配整個單詞,而不是字符串的一部分(如匹配‘magic’,而不是‘magical’),
grep -C number pattern files :匹配的上下文分別顯示[number]行,
grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行,
grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。
這里還有些用于搜索的特殊符號:
\< 和 \> 分別標注單詞的開始與結尾。
例如:
grep man * 會匹配 ‘Batman’、‘manic’、‘man’等,
grep '\<man' * 匹配‘manic’和‘man’,但不是‘Batman’,
grep '\<man\>' 只匹配‘man’,而不是‘Batman’或‘manic’等其他的字符串。
'^':指匹配的字符串在行首,
'$':指匹配的字符串在行尾,
如果您不習慣命令行參數,可以試試圖形界面的‘grep’,如 reXgrep 。這個軟件提供 AND、OR、NOT 等語法,還有漂亮的按鈕 :-) 。如果您只是需要更清楚的輸出,不妨試試 fungrep 。
2014-12-02
grep ,全面搜索正則表達式并把行打印出來)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹配的行打印出來。
grep常用用法:[root@www ~]# grep [-acinv] [--color=auto] '搜尋字符串' filename
選項與參數:
-a :將 binary 文件以 text 文件的方式搜尋數據
-c :計算找到 '搜尋字符串' 的次數
-i :忽略大小寫的不同,所以大小寫視為相同
-n :順便輸出行號
-v :反向選擇,亦即顯示出沒有 '搜尋字符串' 內容的那一行!
2014-12-02
[root@www ~]# grep [-acinv] [--color=auto] '搜尋字符串' filename
選項與參數:-a :將 binary 文件以 text 文件的方式搜尋數據-c :計算找到 '搜尋字符串' 的次數-i :忽略大小寫的不同,所以大小寫視為相同-n :順便輸出行號-v :反向選擇,亦即顯示出沒有 '搜尋字符串' 內容的那一行!--color=auto :可以將找到的關鍵詞部分加上顏色的顯示喔!
2014-12-02
1基礎正則表達式
grep 工具,以前介紹過。
grep -[acinv] '搜索內容串' filename
-a 以文本文件方式搜索
-c 計算找到的符合行的次數
-i 忽略大小寫
-n 順便輸出行號
-v 反向選擇,即找 沒有搜索字符串的行
其中搜索串可以是正則表達式!
1
搜索有the的行,并輸出行號
$grep -n 'the' regular_express.txt
搜 索沒有the的行,并輸出行號
$grep -nv 'the' regular_express.txt
2 利 用[]搜索集合字符
[] 表示其中的某一個字符 ,例如[ade] 表示a或d或e
woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! the soup taste good!
可以用^符號做[]內的前綴,表示除[]內的字符之外的字 符。
比如搜索oo前沒有g的字符串所在的行. 使用 '[^g]oo' 作搜索字符串
woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!
[] 內可以用范圍表示,比如[a-z] 表示小寫字母,[0-9] 表示0~9的數字, [A-Z] 則是大寫字母們。[a-zA-Z0-9]表示所有數字與英文字符。 當然也可以配合^來排除字符。
搜索包含數字的行
woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt
5:However ,this dress is about $ 3183 dollars.
15:You are the best is menu you are the no.1.
行首與行尾字符 ^ $. ^ 表示行的開頭,$表示行的結尾( 不是字符,是位置)那么‘^$' 就表示空行,因為只有
行首和行尾。
這里^與[]里面使用的^意義不同。它表示^后面的串是在行的開頭。
比如搜索the在開頭的行
woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt
12:the symbol '*' is represented as star.
搜索以小寫字母開頭的行
woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as star.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
woody@xiaoc:~/tmp$
搜索開頭不是英文字母的行
woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:#I am VBird
woody@xiaoc:~/tmp$
$表示它前面的串是在行的結尾,比如 '\.' 表示 . 在一行的結尾
搜索末尾是.的行
woody@xiaoc:~/tmp$ grep -n '\.$' regular_express.txt //. 是正則表達式的特殊符號,所以要用\轉義
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
5:However ,this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
.....
注意在MS的系統下生成的文本文件,換行會加上一個 ^M 字符。所以最后的字符會是隱藏的^M ,在處理Windows
下面的文本時要特別注意!
可以用cat dos_file | tr -d '\r' > unix_file 來刪除^M符號。 ^M==\r
那么'^$' 就表示只有行首行尾的空行拉!
搜索空行
woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt
22:
23:
woody@xiaoc:~/tmp$
搜索非空行
woody@xiaoc:~/tmp$ grep -vn '^$' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
..........
任意一個字符. 與重復字符 *
在bash中*代表通配符,用來代表任意個 字符,但是在正則表達式中,他含義不同,*表示有0個或多個 某個字符。
例如 oo*, 表示第一個o一定存在,第二個o可以有一個或多個,也可以沒有,因此代表至少一個o.
點. 代表一個任意字符,必須存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。
woody@xiaoc:~/tmp$ grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! the soup taste good!
16:The world is the same with 'glad'.
woody@xiaoc:~/tmp$
搜索兩個o以上的字符串
woody@xiaoc:~/tmp$ grep -n 'ooo*' regular_express.txt //前兩個o一定存在,第三個o可沒有,也可有多個。
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! the soup taste good!
18:google is the best tools for search keyword.
19:goooooogle yes!
搜索g開頭和結尾,中間是至少一個o的字符串,即gog, goog....gooog...等
woody@xiaoc:~/tmp$ grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
搜索g開頭和結尾的字符串在的行
woody@xiaoc:~/tmp$ grep -n 'g.*g' regular_express.txt // .*表示 0個或多個任意字符
1:"Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
限定連續重復字符的范圍 { }
. * 只能限制0個或多個, 如果要確切的限制字符重復數量,就用{范圍} 。范圍是數字用,隔開 2,5 表示2~5個,
2表示2個,2, 表示2到更多個
注意,由于{ }在SHELL中有特殊意義,因此作為正則表達式用的時候要用\轉義一下。
搜索包含兩個o的字符串的行。
woody@xiaoc:~/tmp$ grep -n 'o\{2\}' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! the soup taste good!
18:google is the best tools for search keyword.
19:goooooogle yes!
搜索g后面跟2~5個o,后面再跟一個g的字符串的行。
woody@xiaoc:~/tmp$ grep -n 'go\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.
搜索包含g后面跟2個以上o,后面再跟g的行。。
woody@xiaoc:~/tmp$ grep -n 'go\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
注意,相讓[]中的^ - 不表現特殊意義,可以放在[]里面內容的后面。
'[^a-z\.!^ -]' 表示沒有小寫字母,沒有. 沒有!, 沒有空格,沒有- 的 串,注意[]里面有個小空格。
另外shell 里面的反向選擇為[!range], 正則里面是 [^range]
2擴展正則表達式
擴展正則表達式是對基礎正則表達式添加了幾個特殊構成的。
它令某些操作更加方便。
比如我們要去除 空白行和行首為 #的行, 會這樣用:
woody@xiaoc:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
............
然而使用支持擴展正則表達式的 egrep 與擴展特殊符號 | ,會方便許多。
注意grep只支持基礎表達式, 而egrep 支持擴展的, 其實 egrep 是 grep -E 的別名而已。因此grep -E 支持擴展正則。
那么:
woody@xiaoc:~/tmp$ egrep -v '^$|^#' regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
....................
這里| 表示或的關系。 即滿足 ^$ 或者 ^# 的字符串。
這里列出幾個擴展特殊符號:
+, 于 . * 作用類似,表示 一個或多個重復字符。
?, 于 . * 作用類似,表示0個或一個字符。
|,表示或關系,比如 'gd|good|dog' 表示有gd,good或dog的串
(),將部分內容合成一個單元組。 比如 要搜索 glad 或 good 可以這樣 'g(la|oo)d'
()的好處是可以對小組使用 + ? * 等。
比如要搜索A和C開頭結尾,中間有至少一個(xyz) 的串,可以這樣 : 'A(xyz)+C'