find grep
find "path" -name/-type/-utime "string" -exec {}\;
grep [option] "string" "path"
l就是文件,r就是行
在find中嵌套grep -l就是文件 -r只是行的內(nèi)容
直接在grep中 -l就是文件, -r是文件+行的內(nèi)容
在當(dāng)前目錄下所有文件中查找內(nèi)容包含 string 的文件:
find ./ -name "*" -exec grep "string" {} \;
注意:在最后不能加 print ,否則會出錯.
在當(dāng)前目錄下所有文件中查找內(nèi)容包含 string 的文件并列出字符所在的文件:
find ./ -name "*" -exec grep -l "string" {} \;
在當(dāng)前目錄下 *.c 中查找內(nèi)容包含 string 的文件并列出字符所在的文件的所在行(不顯示文件名):
find ./ -name "*.c" -exec grep -n "string" {} \;
在當(dāng)前目錄下所有文件中查找內(nèi)容包含 string 的文件并列出字符所在的文件,所在行及所在行的內(nèi)容:
find ./ -name "*" -exec grep -n "string" ./ {} \;
使用 type 選項:f(普通文件) d(目錄) l(符號文件)
如果要在 /etc 目錄下查找所有的目錄:
find /etc -type d -print
如果要在 /etc 目錄下查找 .svn 的目錄:
find /etc -name .svn -type d -print
為了在當(dāng)前目錄下查找除目錄以外的所有類型的文件:
find . ! -type d -print
為了在當(dāng)前目錄下查找所有的符號鏈接文件, 可以用:
find . -type l -print
為了用 ls -l 命令列出所匹配到的文件, 可以把 ls -l 命令放在find命令的 -exec 選項中:
find . -type f -exec ls -l {} \;
find ./ -type f -exec grep -n "#include" ./ {} \;
查看當(dāng)前面目錄下所有文件的grep
grep [-acinv] [--color=auto] '搜尋字符串' filename
選項與參數(shù):
-a :將 binary 文件以 text 文件的方式搜尋數(shù)據(jù)
-c :計算找到 '搜尋字符串' 的次數(shù)
-i :忽略大小寫的不同,所以大小寫視為相同
-n :順便輸出行號
-v :反向選擇,亦即顯示出沒有 '搜尋字符串' 內(nèi)容的那一行!
--color=auto :可以將找到的關(guān)鍵詞部分加上顏色的顯示喔!
根據(jù)文件內(nèi)容遞歸查找目錄
# grep ‘energywise’ * #在當(dāng)前目錄搜索帶'energywise'行的文件
# grep -r ‘energywise’ * #在當(dāng)前目錄及其子目錄下搜索'energywise'行的文件+所在行的內(nèi)容
# grep -l ‘energywise’ * #在當(dāng)前目錄及其子目錄下搜索'energywise'行的文件
字符類
字符類的搜索:如果我想要搜尋 test 或 taste 這兩個單字時,可以發(fā)現(xiàn)到,其實她們有共通的 't?st' 存在~這個時候,我可以這樣來搜尋:
[root@www ~]# grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
其實 [] 里面不論有幾個字節(jié),他都謹(jǐn)代表某『一個』字節(jié), 所以,上面的例子說明了,我需要的字串是『tast』或『test』兩個字串而已!
字符類的反向選擇 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下
注意這個文件是文件,不是目錄
[root@www ~]# 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!
字符類的連續(xù):再來,假設(shè)我 oo 前面不想要有小寫字節(jié),所以,我可以這樣寫 [^abcd....z]oo , 但是這樣似乎不怎么方便,由於小寫字節(jié)的 ASCII 上編碼的順序是連續(xù)的, 因此,我們可以將之簡化為底下這樣:
[root@www ~]# grep -n '[^a-z]oo' regular_express.txt
3:Football game is not use feet only.
行首與行尾字節(jié) ^ $
行首字符:如果我想要讓 the 只在行首列出呢? 這個時候就得要使用定位字節(jié)了!我們可以這樣做:
[root@www ~]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start.
此時,就只剩下第 12 行,因為只有第 12 行的行首是 the 開頭啊~此外, 如果我想要開頭是小寫字節(jié)的那一行就列出呢?可以這樣:
[root@www ~]# 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 start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
如果我不想要開頭是英文字母,則可以是這樣:
[root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird
^ 符號,在字符類符號(括號[])之內(nèi)與之外是不同的! 在 [] 內(nèi)代表『反向選擇』,在 [] 之外則代表定位在行首的意義!
找出空白行:
[root@www ~]# grep -n '^$' regular_express.txt
22:
因為只有行首跟行尾 (^$),所以,這樣就可以找出空白行啦!
任意一個字節(jié) . 與重復(fù)字節(jié) *
這兩個符號在正則表達式的意義如下:
. (小數(shù)點):代表『一定有一個任意字節(jié)』的意思;
* (星號):代表『重復(fù)前一個字符, 0 到無窮多次』的意思,為組合形態(tài)
假設(shè)我需要找出 g??d 的字串,亦即共有四個字節(jié), 起頭是 g 而結(jié)束是 d ,我可以這樣做:
[root@www ~]# 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 <Happy> is the same with "glad".
因為強調(diào) g 與 d 之間一定要存在兩個字節(jié),因此,第 13 行的 god 與第 14 行的 gd 就不會被列出來啦!
如果我想要列出有 oo, ooo, oooo 等等的數(shù)據(jù), 也就是說,至少要有兩個(含) o 以上,該如何是好?
因為 * 代表的是『重復(fù) 0 個或多個前面的 RE 字符』的意義, 因此,『o*』代表的是:『擁有空字節(jié)或一個 o 以上的字節(jié)』,因此,『 grep -n 'o*' regular_express.txt 』將會把所有的數(shù)據(jù)都列印出來終端上!
當(dāng)我們需要『至少兩個 o 以上的字串』時,就需要 ooo* ,亦即是:
[root@www ~]# grep -n 'ooo*' 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!
如果我想要字串開頭與結(jié)尾都是 g,但是兩個 g 之間僅能存在至少一個 o ,亦即是 gog, goog, gooog.... 等等,那該如何?
[root@www ~]# grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
如果我想要找出 g 開頭與 g 結(jié)尾的行,當(dāng)中的字符可有可無
[root@www ~]# grep -n 'g.*g' regular_express.txt
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.
如果我想要找出『任意數(shù)字』的行?因為僅有數(shù)字,所以就成為:
[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
限定連續(xù) RE 字符范圍 {}
我們可以利用 . 與 RE 字符及 * 來配置 0 個到無限多個重復(fù)字節(jié), 那如果我想要限制一個范圍區(qū)間內(nèi)的重復(fù)字節(jié)數(shù)呢?
舉例來說,我想要找出兩個到五個 o 的連續(xù)字串,該如何作?這時候就得要使用到限定范圍的字符 {} 了。 但因為 { 與 } 的符號在 shell 是有特殊意義的,因此, 我們必須要使用字符 \ 來讓他失去特殊意義才行。 至於 {} 的語法是這樣的,假設(shè)我要找到兩個 o 的字串,可以是:
[root@www ~]# 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 ke
19:goooooogle yes!
假設(shè)我們要找出 g 后面接 2 到 5 個 o ,然后再接一個 g 的字串,他會是這樣:
[root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.
如果我想要的是 2 個 o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是:
[root@www ~]# grep -n 'go\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
在當(dāng)前目錄下所有文件中查找內(nèi)容包含 string 的文件:find ./ -name "*" -exec grep "string" {} \;注意:在最后不能加 print ,否則會出錯. 在當(dāng)前目錄下所有文件中查找內(nèi)容包含 string 的文件并列出字符所在的文件:find ./ -name "*" -exec grep -l "string" {} \; 在當(dāng)前目錄下 *.c 中查找內(nèi)容包含 string 的文件并列出字符所在的文件的所在行(不顯示文件名):find ./ -name "*.c" -exec grep -n "string" {} \; 在當(dāng)前目錄下所有文件中查找內(nèi)容包含 string 的文件并列出字符所在的文件,所在行及所在行的內(nèi)容:find ./ -name "*" -exec grep -n "string" ./ {} \;
使用 type 選項:f(普通文件) d(目錄) l(符號文件)如果要在 /etc 目錄下查找所有的目錄:find /etc -type d -print 如果要在 /etc 目錄下查找 .svn 的目錄:find /etc -name .svn -type d -print
為了在當(dāng)前目錄下查找除目錄以外的所有類型的文件:find . ! -type d -print 為了在當(dāng)前目錄下查找所有的符號鏈接文件, 可以用:find . -type l -print
為了用 ls -l 命令列出所匹配到的文件, 可以把 ls -l 命令放在find命令的 -exec 選項中:find . -type f -exec ls -l {} \;
grep [-acinv] [--color=auto] '搜尋字符串' filename選項與參數(shù):-a :將 binary 文件以 text 文件的方式搜尋數(shù)據(jù)-c :計算找到 '搜尋字符串' 的次數(shù)-i :忽略大小寫的不同,所以大小寫視為相同-n :順便輸出行號-v :反向選擇,亦即顯示出沒有 '搜尋字符串' 內(nèi)容的那一行!--color=auto :可以將找到的關(guān)鍵詞部分加上顏色的顯示喔!
根據(jù)文件內(nèi)容遞歸查找目錄# grep ‘energywise’ * #在當(dāng)前目錄搜索帶'energywise'行的文件# grep -r ‘energywise’ * #在當(dāng)前目錄及其子目錄下搜索'energywise'行的文件# grep -l -r ‘energywise’ * #在當(dāng)前目錄及其子目錄下搜索'energywise'行的文件,但是不顯示匹配的行,只顯示匹配的文件
字符類字符類的搜索:如果我想要搜尋 test 或 taste 這兩個單字時,可以發(fā)現(xiàn)到,其實她們有共通的 't?st' 存在~這個時候,我可以這樣來搜尋:
[root@www ~]# grep -n 't[ae]st' regular_express.txt8:I can't finish the test.9:Oh! The soup taste good.
其實 [] 里面不論有幾個字節(jié),他都謹(jǐn)代表某『一個』字節(jié), 所以,上面的例子說明了,我需要的字串是『tast』或『test』兩個字串而已!
字符類的反向選擇 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下注意這個文件是文件,不是目錄[root@www ~]# grep -n '[^g]oo' regular_express.txt2: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!
字符類的連續(xù):再來,假設(shè)我 oo 前面不想要有小寫字節(jié),所以,我可以這樣寫 [^abcd....z]oo , 但是這樣似乎不怎么方便,由於小寫字節(jié)的 ASCII 上編碼的順序是連續(xù)的, 因此,我們可以將之簡化為底下這樣:
[root@www ~]# grep -n '[^a-z]oo' regular_express.txt3:Football game is not use feet only.
行首與行尾字節(jié) ^ $行首字符:如果我想要讓 the 只在行首列出呢? 這個時候就得要使用定位字節(jié)了!我們可以這樣做:
[root@www ~]# grep -n '^the' regular_express.txt12:the symbol '*' is represented as start.
此時,就只剩下第 12 行,因為只有第 12 行的行首是 the 開頭啊~此外, 如果我想要開頭是小寫字節(jié)的那一行就列出呢?可以這樣:
[root@www ~]# grep -n '^[a-z]' regular_express.txt2:apple is my favorite food.4:this dress doesn't fit me.10:motorcycle is cheap than car.12:the symbol '*' is represented as start.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.
如果我不想要開頭是英文字母,則可以是這樣:
[root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt1:"Open Source" is a good mechanism to develop programs.21:# I am VBird^ 符號,在字符類符號(括號[])之內(nèi)與之外是不同的! 在 [] 內(nèi)代表『反向選擇』,在 [] 之外則代表定位在行首的意義!
找出空白行:
[root@www ~]# grep -n '^$' regular_express.txt22:因為只有行首跟行尾 (^$),所以,這樣就可以找出空白行啦!
任意一個字節(jié) . 與重復(fù)字節(jié) *這兩個符號在正則表達式的意義如下:
. (小數(shù)點):代表『一定有一個任意字節(jié)』的意思;* (星號):代表『重復(fù)前一個字符, 0 到無窮多次』的意思,為組合形態(tài)假設(shè)我需要找出 g??d 的字串,亦即共有四個字節(jié), 起頭是 g 而結(jié)束是 d ,我可以這樣做:
[root@www ~]# grep -n 'g..d' regular_express.txt1:"Open Source" is a good mechanism to develop programs.9:Oh! The soup taste good.16:The world <Happy> is the same with "glad".因為強調(diào) g 與 d 之間一定要存在兩個字節(jié),因此,第 13 行的 god 與第 14 行的 gd 就不會被列出來啦!
如果我想要列出有 oo, ooo, oooo 等等的數(shù)據(jù), 也就是說,至少要有兩個(含) o 以上,該如何是好?
因為 * 代表的是『重復(fù) 0 個或多個前面的 RE 字符』的意義, 因此,『o*』代表的是:『擁有空字節(jié)或一個 o 以上的字節(jié)』,因此,『 grep -n 'o*' regular_express.txt 』將會把所有的數(shù)據(jù)都列印出來終端上!
當(dāng)我們需要『至少兩個 o 以上的字串』時,就需要 ooo* ,亦即是:[root@www ~]# grep -n 'ooo*' regular_express.txt1:"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!
如果我想要字串開頭與結(jié)尾都是 g,但是兩個 g 之間僅能存在至少一個 o ,亦即是 gog, goog, gooog.... 等等,那該如何?[root@www ~]# grep -n 'goo*g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!
如果我想要找出 g 開頭與 g 結(jié)尾的行,當(dāng)中的字符可有可無[root@www ~]# grep -n 'g.*g' regular_express.txt1:"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.
如果我想要找出『任意數(shù)字』的行?因為僅有數(shù)字,所以就成為:
[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.
限定連續(xù) RE 字符范圍 {}我們可以利用 . 與 RE 字符及 * 來配置 0 個到無限多個重復(fù)字節(jié), 那如果我想要限制一個范圍區(qū)間內(nèi)的重復(fù)字節(jié)數(shù)呢?
舉例來說,我想要找出兩個到五個 o 的連續(xù)字串,該如何作?這時候就得要使用到限定范圍的字符 {} 了。 但因為 { 與 } 的符號在 shell 是有特殊意義的,因此, 我們必須要使用字符 \ 來讓他失去特殊意義才行。 至於 {} 的語法是這樣的,假設(shè)我要找到兩個 o 的字串,可以是:
[root@www ~]# grep -n 'o\{2\}' regular_express.txt1:"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 ke19:goooooogle yes!
假設(shè)我們要找出 g 后面接 2 到 5 個 o ,然后再接一個 g 的字串,他會是這樣:
[root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt18:google is the best tools for search keyword.
如果我想要的是 2 個 o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是:
[root@www ~]# grep -n 'go\{2,\}g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!

浙公網(wǎng)安備 33010602011771號