在Linux下,我們經(jīng)常會(huì)使用grep對一些結(jié)果進(jìn)行過過濾,下面簡單介紹一個(gè)grep和正則表達(dá)式結(jié)合的用法。
源文件如下:
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.$
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.$
11 This window is clear.$
12 the symbol '*' is represented as start.$
13 Oh!^IMy god!$
14 The gd software is a library for drafting programs.^M$
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.$
19 goooooogle yes!$
20 go! go! Let's go.$
21 # I am VBird$
22 $
-
查找特定字符串
- 查找the這個(gè)特定字符串:grep -n 'the' regular_express.txt
grep -n 'the' regular_express.txt-
查找不包含the這個(gè)字符的行:grep -vn 'the' regular_express.txt
grep -vn 'the' regular_express.txt -
忽略大小寫查找包含the的行:grep -in 'the' regular_express.txt
grep -in 'the' regular_express.txt
-
利用中括號[]來查找集合字符
-
比如我想查找test或者taste這兩個(gè)單詞時(shí),可以這么寫:grep -n 't[ae]st' regular_express.txt (不論方括號里面有幾個(gè)字符,它都只代表一個(gè)字符
grep -n 't[ae]st' regular_express.txt -
如果不想要oo前面有g(shù)的話,可以這么寫:grep -n '[^g]oo' regular_express.txt ([^g]中的^表示反向選擇,即表示oo前面不能是g)
grep -n '[^g]oo' regular_express.txt -
假如我oo前面不能有小寫字符:grep -n '[^a-z]oo' regular_express.txt (a-z就表示小寫字符,還有另外一種寫法使用特殊字符[:lower:],這個(gè)也是表示小寫字母-grep -n '[^[:lower:]]oo' regular_express.txt)
grep -n '[^a-z]oo' regular_express.txt
-
-
行首和行尾字符^ 和 $
-
查找到一行中行首為the的:grep -n '^the' regular_express.txt
grep -n '^the' regular_express.txt -
查找不以小寫字符開頭的:grep -n '[a-z]' regular_express.txt(這里需要注意^ 在[]的里面和外面是兩個(gè)不同的概念,在[]里面表示反向選擇,在[]外面表示定位在行首的意義)
grep -n '^[^a-z]' regular_express.txt -
查找行尾是. d的: grep -n '.$' regular_express.txt, 這里有兩個(gè)點(diǎn)需要注意第一是小數(shù)點(diǎn).之前有一個(gè)轉(zhuǎn)移符,這是因?yàn)?具有其他意義,因此需要使用轉(zhuǎn)移符\來消除其特殊意義。第二點(diǎn)就是文件中的第5-9行并沒有被打印出來。我們?nèi)绻屑?xì)看源文件的話或發(fā)現(xiàn)第5-9行的結(jié)尾是.^M$,這里涉及到一個(gè)Linux和windows的斷行字符的問題,Linux是以LF(\n)斷行,而windows是以CRLF(\r\n)斷行,所以一般我們在Linux上面查看windows的文件,其結(jié)尾都是^M,所以我們在windows下編寫的腳本放到Linux上運(yùn)行之前需要小心,要去除掉這個(gè)字符,不然運(yùn)行會(huì)報(bào)錯(cuò)。
grep -n '.$' regular_express.txt
-








