linux中grep用法詳解,linuxgrep用法詳解

來源:互聯網
上載者:User

linux中grep用法詳解,linuxgrep用法詳解

尋找特定字串並顏色顯示
[root@fwq test]# grep -n 'the' regular_express.txt --color=auto
8:I can't finish the test.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.

 

反向選擇尋找特定字串並顏色顯示

[root@fwq test]# grep -vn 'the' regular_express.txt --color=auto
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
......

忽略大小寫尋找特定的字元

 [root@fwq test]#  grep -in 'the' regular_express.txt

使用 [] 來尋找集合字元:

[root@fwq test]# grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.

尋找有‘oo’字串

[root@fwq test]# grep -n 'oo' 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!

尋找有‘oo’字串,但是不要前面有g的,即剔除goo

[root@fwq test]# 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!

尋找非小寫字母加oo的內容

[root@fwq test]# grep -n '[^a-z]oo' regular_express.txt
3:Football game is not use feet only.


 

擷取有數位一行

[root@fwq test]# grep -n '[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.

[root@fwq test]# grep -n '[^[:lower:]]oo' regular_express.txt
3:Football game is not use feet only.

 

查詢以the開頭的字元

[root@fwq test]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start.

查詢開頭是小寫字母的內容

[root@fwq test]# 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@fwq test]# grep -n '^[[:lower:]]' 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@fwq test]# grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird

查詢結尾是小數點的行的內容.

[root@fwq test]# 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.
10:motorcycle is cheap than car.
11:This window is clear.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
20:go! go! Let's go.

尋找“空白行”

[root@fwq test]# grep -n '^$' regular_express.txt
22:

萬用字元.和*的使用,.(小數點)代表一定有一個任一字元,*代表重複前一個到無窮多次的意思

[root@fwq test]# 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".

查詢出現任一數字的行列

[root@fwq test]# 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.

 

查詢出現兩個o的字串

[root@fwq test]# 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!

查詢出現2-5個o的字串,後面在接一個g的字串

[root@fwq test]# grep -n 'o\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!

查詢出現2個以上o的字串,後面在接一個g的字串

[root@fwq test]# grep -n 'o\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!


 

 

 

 

 

 

 

 

 

 

 


 


 


Linux下Grep命令的詳細使用方法

在linux中grep命令是非常有用的,它和管道(|)配合使用,非常強大,用於搜尋文字檔.如果想要在幾個文字檔中尋找一字串,可以使用‘grep’命令。‘grep’在文本中搜尋指定的字串。
假設您正在‘/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 等文法,還有漂亮的按鈕 ......餘下全文>>
 
linux中grep的用法

ls或者其他命令|grep 過濾的東西
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.