P350php
grep -n 列出行數linux
[root@www ~]# grep [-A] [-B] [--color=auto] '搜尋字串' filename 選項與參數: -A :後面可加數字,爲 after 的意思,除了列出該行外,後續的 n 行也列出來; -B :後面可加數字,爲 befer 的意思,除了列出該行外,前面的 n 行也列出來; --color=auto 可將正確的那個擷取數據列出顏色 範例一:用 dmesg 列出核心信息,再以 grep 找出內含 eth 那行 [root@www ~]# dmesg | grep 'eth' eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10 eth0: Identified 8139 chip type 'RTL-8139C' eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1 eth0: no IPv6 routers present # dmesg 可列出核心產生的信息!透過 grep 來擷取網絡卡相關資訊 (eth) , # 就可發現如上資訊。不過沒有行號與特殊顏色顯示!看看下個範例吧! 範例二:承上題,要將捉到的關鍵字顯色,且加上行號來表示: [root@www ~]# dmesg | grep -n --color=auto 'eth' 247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10 248:eth0: Identified 8139 chip type 'RTL-8139C' 294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1 305:eth0: no IPv6 routers present # 你會發現除了 eth 會有特殊顏色來表示以外,最前面還有行號喔! 範例三:承上題,在關鍵字所在行的前兩行與後三行也一塊兒捉出來顯示 [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 DVD-ROM DVD-R-RAM CD-R/RW drive, 2048kB Cache, UDMA(66) # 如上所示,你會發現關鍵字 247 所在的前兩行及 248 後三行也都被顯示出來! # 這樣可讓你將關鍵字先後數據捉出來進行分析啦!
grep 在數據中查尋一個字串時,是以 "整行"
爲單位來進行數據的擷取的!』也就是說,假如一個文件內有 10
行,其中有兩行具備你所搜尋的字串,則將那兩行顯示在螢幕上,其餘的就丟棄了!
若是你想要取得不論大小寫的 the 這個字串,則:
[root@www ~]# grep -in 'the' regular_express.txt 8:I can't finish the test. 9:Oh! The soup taste good. 12:the symbol '*' is represented as start. 14:The gd software is a library for drafting programs. 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. |
例題2、利用中括號 [] 來搜尋集合字節
若是我想要搜尋 test 或 taste 這兩個單字時,能夠發現到,其實她們有共通的 't?st' 存在~這個時候,我能夠這樣來搜尋:git
[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 的字節時,則使用:shell
[root@www ~]# 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 的話呢?此時,能夠利用在集合字節的反向選擇 [^] 來達成:express
[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! |
意思就是說,我須要的是 oo ,可是 oo 前面不能是 g 就是了!網絡
當咱們在一組集合字節中,若是該字節組是連續的,例如大寫英文/小寫英文/數字等等,
就可使用[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 '[^[:lower:]]oo' regular_express.txt # 那個 [:lower:] 表明的就是 a-z 的意思!請參考前兩小節的說明表格 [root@www ~]# grep -n '[[:digit:]]' regular_express.txt |
例題3、行首與行尾字節 ^ $
咱們在例題一當中,能夠查詢到一行字串裏面有 the 的,那若是我想要讓 the 只在行首列出呢? 這個時候就得要使用定位字節了!咱們能夠這樣作:app
[root@www ~]# grep -n '^the' regular_express.txt 12:the symbol '*' is represented as start. |
此時,就只剩下第 12 行,由於只有第 12 行的行首是 the 開頭啊~此外, 若是我想要開頭是小寫字節的那一行就列出呢?能夠這樣:測試
[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. |
你能夠發現咱們能夠捉到第一個字節都不是大寫的!只不過 grep 列出的關鍵字部分不僅有第一個字節, grep 是列出一整個字 (word) 說!一樣的,上面的命令也能夠用以下的方式來取代的:this
[root@www ~]# grep -n '^[[:lower:]]' regular_express.txt
|
好!那若是我不想要開頭是英文字母,則能夠是這樣:google
[root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 21:# I am VBird # 命令也能夠是: grep -n '^[^[:alpha:]]' regular_express.txt |
注意到了吧?那個 ^ 符號,在字節集合符號(括號[])以內與以外是不一樣的! 在 [] 內表明『反向選擇』,在 [] 以外則表明定位在行首的意義!要分清楚喔! 反過來思考,那若是我想要找出來,行尾結束爲小數點 (.) 的那一行,該如何處理:
[root@www ~]# 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. |
特別注意到,由於小數點具備其餘意義(底下會介紹),因此必需要使用跳脫字節(\)來加以解除其特殊意義! 不過,你或許會以爲奇怪,可是第 5~9 行最後面也是 . 啊~怎麼沒法列印出來? 這裏就牽涉到 Windows 平臺的軟件對於斷行字節的判斷問題了!咱們使用 cat -A 將第五行拿出來看, 你會發現:
[root@www ~]# cat -An regular_express.txt | head -n 10 | tail -n 6 5 However, this dress is about $ 3183 dollars.^M$ 6 GNU is free air not free beer.^M$ 7 Her hair is very beauty.^M$ 8 I can't finish the test.^M$ 9 Oh! The soup taste good.^M$ 10 motorcycle is cheap than car.$ |
咱們在第十章內談到過斷行字節在 Linux 與 Windows 上的差別, 在上面的表格中咱們能夠發現 5~9 行爲 Windows 的斷行字節 (^M$) ,而正常的 Linux 應該僅有第 10 行顯示的那樣 ($) 。因此羅,那個 . 天然就不是緊接在 $ 以前喔!也就捉不到 5~9 行了!這樣能夠了解 ^ 與 $ 的意義嗎? 好了,先不要看底下的解答,本身想想,那麼若是我想要找出來,哪一行是『空白行』, 也就是說,該行並無輸入任何數據,該如何搜尋?
[root@www ~]# grep -n '^$' regular_express.txt
22:
|
由於只有行首跟行尾 (^$),因此,這樣就能夠找出空白行啦!再來,假設你已經知道在一個程序腳本 (shell script) 或者是配置檔當中,空白行與開頭爲 # 的那一行是註解,所以若是你要將數據列出給別人參考時, 能夠將這些數據省略掉以節省保貴的紙張,那麼你能夠怎麼做呢? 咱們以 /etc/syslog.conf 這個文件來做範例,你能夠自行參考一下輸出的結果:
[root@www ~]# cat -n /etc/syslog.conf # 在 CentOS 中,結果能夠發現有 33 行的輸出,不少空白行與 # 開頭 [root@www ~]# grep -v '^$' /etc/syslog.conf | grep -v '^#' # 結果僅有 10 行,其中第一個『 -v '^$' 』表明『不要空白行』, # 第二個『 -v '^#' 』表明『不要開頭是 # 的那行』喔! |