Linux學習之grep命令

2021-07-11 00:28:04 字數 3847 閱讀 7204

參考資料:

[root@www ~]# dmesg | grep -n -a3 -b2 –color=auto 『eth』

245-pci: setting irq 10 as level-triggered

246-acpi: pci interrupt 0000:00:0e.0[a] -> link [lnkb] …

247:eth0: realtek rtl8139 at 0xee846000, 00:90:cc:a6:34:84, irq 10

248:eth0: identified 8139 chip type 『rtl-8139c』

249-input: pc speaker as /class/input/input2

250-acpi: pci interrupt 0000:00:01.4[b] -> link [lnkb] …

251-hdb: atapi 48x ***-rom ***-r-ram cd-r/rw drive, 2048kb cache, udma(66)

如上所示,你會發現關鍵字 247 所在的前兩行及 248 後三行也都被顯示出來!

這樣可以讓你將關鍵字前後資料捉出來進行分析啦!

6) 根據檔案內容遞迴查詢目錄

grep 『energywise』 * #在當前目錄搜尋帶』energywise』行的檔案

grep -r 『energywise』 * #在當前目錄及其子目錄下搜尋』energywise』行的檔案

grep -l -r 『energywise』 * #在當前目錄及其子目錄下搜尋』energywise』行的檔案,但是不顯示匹配的行,

只顯示匹配的檔案

7) grep與正規表示式

字元類

字元類的搜尋:如果我想要搜尋 test 或 taste 這兩個單字時,可以發現到,其實她們有共通的 『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.

其實 裡面不論有幾個位元組,他都謹代表某『乙個』位元組, 所以,上面的例子說明了,我需要的字串是『tast』或『test』兩個字串而已!

字元類的反向選擇 [^] :如果想要搜尋到有 oo 的行,但不想要 oo 前面有 g,如下

字元類的連續:再來,假設我 oo 前面不想要有小寫位元組,所以,我可以這樣寫 [^abcd….z]oo , 但是這樣似乎不怎麼方便,由於小寫位元組的 ascii 上編碼的順序是連續的, 因此,我們可以將之簡化為底下這樣:

[root@www ~]# grep -n 『[^a-z]oo』 regular_express.txt

3:football game is not use feet only.

也就是說,當我們在一組集合位元組中,如果該位元組組是連續的,例如大寫英文/小寫英文/數字等等, 就可以使用[a-z],[a-z],[0-9]等方式來書寫,那麼如果我們的要求字串是數字與英文呢? 呵呵!就將他全部寫在一起,變成:[a-za-z0-9]。

我們要取得有數字的那一行,就這樣:

[root@www ~]# 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@www ~]# grep -n '^$'   regular_express.txt

22:

因為只有行首跟行尾 (^$),所以,這樣就可以找出空白行啦!

任意乙個位元組 . 與重複位元組 *

這兩個符號在正規表示式的意義如下:

. (小數點):代表『一定有乙個任意位元組』的意思;

* (星號):代表『重複前乙個字元, 0 到無窮多次』的意思,為組合形態

假設我需要找出 g??d 的字串,亦即共有四個位元組, 起頭是 g 而結束是 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 is the same with 「glad」.

因為強調 g 與 d 之間一定要存在兩個位元組,因此,第 13 行的 god 與第 14 行的 gd 就不會被列出來啦!

如果我想要列出有 oo, ooo, oooo 等等的資料, 也就是說,至少要有兩個(含) o 以上,該如何是好?

因為 * 代表的是『重複 0 個或多個前面的 re 字元』的意義, 因此,『o*』代表的是:『擁有空位元組或乙個 o 以上的位元組』,因此,『 grep -n 『o*』 regular_express.txt 』將會把所有的資料都列印出來終端上!

當我們需要『至少兩個 o 以上的字串』時,就需要 ooo* ,亦即是:

如果我想要找出 g 開頭與 g 結尾的行,當中的字元可有可無

[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.

因為是代表 g 開頭與 g 結尾,中間任意位元組均可接受,所以,第 1, 14, 20 行是可接受的喔! 這個 .* 的 re 表示任意字元是很常見的.

如果我想要找出『任意數字』的行?因為僅有數字,所以就成為:

[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.

限定連續 re 字元範圍 {}

我們可以利用 . 與 re 字元及 * 來配置 0 個到無限多個重複位元組, 那如果我想要限制乙個範圍區間內的重複位元組數呢?

舉例來說,我想要找出兩個到五個 o 的連續字串,該如何作?這時候就得要使用到限定範圍的字元 {} 了。 但因為 的符號在 shell 是有特殊意義的,因此, 我們必須要使用字元 \ 來讓他失去特殊意義才行。 至於 {} 的語法是這樣的,假設我要找到兩個 o 的字串,可以是:

假設我們要找出 g 後面接 2 到 5 個 o ,然後再接乙個 g 的字串,他會是這樣:

[root@www ~]# grep -n 『gog』 regular_express.txt

18:google is the best tools for search keyword.

如果我想要的是 2 個 o 以上的 goooo….g 呢?除了可以是 gooo*g ,也可以是:

[root@www ~]# grep -n 『gog』 regular_express.txt

18:google is the best tools for search keyword.

19:goooooogle yes!

Linux命令學習之grep命令

格式 grep options pattern file grep命令堆在輸入或指定的檔案中查詢包含匹配指定模式的字元的行。grep的輸出就是包含了匹配模式的行。原始檔內容 ocetl yxddn65 demo more file12 46315 9510 ocetl yxddn65 demo gr...

linux之cp命令grep命令學習

cp命令就是在linux下對檔案複製的乙個命令,功能很強大啊。其命令格式如下 cp 複製檔案或目錄 cp adfilprsu source destination cp options source1 source2 source3 directory 引數 a 相當於 pdr 的意思 d 若 檔案...

Linux命令之 grep命令》

1.作用 linux系統中grep命令是一種強大的文字搜尋工具,它能使用正規表示式搜尋文字,並把匹配的行列印出來 也可以通過管道重定向流輸出到檔案中 grep全稱是global regular expression print,表示全域性正規表示式列印,它的使用許可權是所有使用者。2.格式 grep...