例題3、行首與行尾字元 ^ $php
我們在例題一當中,能夠查詢到一行字串裡面有 the 的,那若是我想要讓 the 只在行首列出呢? 這個時候就得要使用定位字元了!我們能夠這樣作:linux
[dmtsai@study ~]$ grep -n '^the' regular_express.txt 12:the symbol '*' is represented as start.
此時,就只剩下第 12 行,因為只有第 12 行的行首是 the 開頭啊~此外, 若是我想要開頭是小寫字元的那一行就列出呢?能夠這樣:shell
[dmtsai@study ~]$ 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.
你能夠發現我們能夠捉到第一個字元都不是大寫的!上面的指令也能夠用以下的方式來取代的:express
[dmtsai@study ~]$ grep -n '^[[:lower:]]' regular_express.txt
好!那若是我不想要開頭是英文字母,則能夠是這樣:app
[dmtsai@study ~]$ grep -n '^[^a-zA-Z]' regular_express.txt1:"Open Source" is a good mechanism to develop programs. 21:# I am VBird# 指令也能夠是: grep -n '^[^[:alpha:]]' regular_express.txt
注意到了吧?那個 ^ 符號,在字元集合符號(括號[])之內與以外是不一樣的! 在 [] 內表明『反向選擇』,在 [] 以外則表明定位在行首的意義!要分清楚喔! 反過來思考,那若是我想要找出來,行尾結束為小數點 (.) 的那一行,該如何處理:this
[dmtsai@study ~]$ 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 將第五行拿出來看, 你會發現:google
[dmtsai@study ~]$ 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 行了!這樣能夠瞭解 ^ 與 $ 的意義嗎? 好了,先不要看底下的解答,本身想想,那麼若是我想要找出來,哪一行是『空白行』, 也就是說,該行並沒有輸入任何資料,該如何搜尋?spa
[dmtsai@study ~]$ grep -n '^$' regular_express.txt22:
因為只有行首跟行尾 (^$),因此,這樣就能夠找出空白行啦!再來,假設你已經知道在一個程式腳本 (shell script) 或者是設定檔當中,空白行與開頭為 # 的那一行是註解,所以若是你要將資料列出給別人參考時, 能夠將這些資料省略掉以節省保貴的紙張,那麼你能夠怎麼做呢? 我們以 /etc/rsyslog.conf 這個檔案來做範例,你能夠自行參考一下輸出的結果:
code
[dmtsai@study ~]$ cat -n /etc/rsyslog.conf# 在 CentOS 7 中,結果能夠發現有 91 行的輸出,不少空白行與 # 開頭的註解行[dmtsai@study ~]$ grep -v '^$' /etc/rsyslog.conf | grep -v '^#'# 結果僅有 14 行,其中第一個『 -v '^$' 』表明『不要空白行』, # 第二個『 -v '^#' 』表明『不要開頭是 # 的那行』喔!
是否節省不少版面啊?另外,你可能也會問,那為何不要出現 # 的符號的那行就直接捨棄呢?沒辦法!因為某些註解是與設定寫在同一行的後面, 若是你只是抓 # 就予以去除,那就會將某些設定也同時移除了!那錯誤就大了~
orm