linux-grep

--version or -V grep的版本

-A 數字N找到所有的匹配行,並顯示匹配行後N

-B 數字N找到所有的匹配行,並顯示匹配行前面N

-b顯示匹配到的字符在文件中的偏移地址

-c顯示有多少行被匹配到

--color把匹配到的字符用顏色顯示出來

-e可以使用多個正則表達式

-f FILEA FILEB FILEAFILEAB中的匹配

-i不區分大小寫針對單個字符

-m 數字N最多匹配N個後停止

-n打印行號

-o只打印出匹配到的字符

-R搜索子目錄

-v顯示不包括查找字符的所有行




[root@localhost test]# cat test.txt 

a

b

c

d

123

456

789

aaa

bbb

ccc

ddd

eee

(*%#@^%$)

aa

bb

cc

dd

1234567890

qwertyuiop

asdfghjkl

zxcvbnm


1.-A

找到所有的匹配行,並顯示匹配行後面N

格式:grep -A number document

[root@localhost test]# grep -A  1 a test.txt 

a

b

--

aaa

bbb

--

aa

bb

--

asdfghjkl

zxcvbnm


2.-B 

格式:grep -B number document

找到所有匹配的行,並顯示匹配行前面N

[root@localhost test]# grep -B 1 c test.txt 

b

c

--

bbb

ccc

--

bb

cc

--

asdfghjkl

zxcvbnm


3.-b

格式:grep -b text document

顯示匹配到的字符在文件中的偏移地址

[root@localhost test]# grep -b a test.txt 

0:a

20:aaa

50:aa

84:asdfghjkl


4.-c

格式:grep -c text document

顯示有多少行被匹配到

[root@localhost test]# grep -c a test.txt 

4


5.--color

把匹配到的字符用顏色顯示出來

[root@localhost test]# grep --color b test.txt 

Screen Shot 2017-10-11 at 2.40.55 PM.png


6.-e

可以使用多個正則表達式

[root@localhost test]# grep -e a -e b test.txt 

a

b

aaa

bbb

aa

bb

asdfghjkl

zxcvbnm


7.-f

FILE_AFILEA_B中的匹配

[root@localhost test]# vi t1

a

b

AA

BB

1234567

[root@localhost test]# vi t2

b

BB

abc

[root@localhost test]# grep -f t1 t2 

b

BB

abc


8.-i

不區分大小寫

[root@localhost test]# grep -i a t1

a

AA


9.-m

最多匹配N個後停止,實際上一般是匹配到N個後的那一行停止

[root@localhost test]# grep -m 2 a test.txt 

a

aaa


10.-n

顯示行號

[root@localhost test]# grep -m 2 a -n test.txt 

1:a

8:aaa


11.-o

只會打印匹配到的字符

[root@localhost test]# grep -o a test.txt 

a

a

a

a

a

a

a



12.-R


只會在當前目錄查找

[root@localhost test]# grep "a" *

a.sh:aa bb

cc.log:aaa

cc.log:a

t1:a

t2:abc

test.txt:a

test.txt:aaa

test.txt:aa

test.txt:asdfghjkl



在當前目錄以及子目錄查找

[root@localhost test]# grep -R aa *

a.sh:aa bb

cc.log:aaa

test.txt:aaa

test.txt:aa


顯示不包括查找字符的所有行

[root@localhost test]# grep -v AA t1 

a

b

BB

1234567



—————————————

pattern主要參數

參數解釋

^ 匹配行首

$ 匹配行尾

[ ] or [ n - n ] 匹配[ ]內字符

.匹配任意的單字符

*緊跟一個單字符,表示匹配0個或者多個此字符

\用來屏蔽元字符的特殊含義

\?匹配前面的字符0次或者1

\+匹配前面的字符1次或者多次

X\{m\} 匹配字符X m

X\{m,\} 匹配字符X最少m

X\{m,n\} 匹配字符X m---n

666標記匹配字符,如666被標記爲1,隨後想使用666,直接以 1代替即可

\|表示或的關係


【案例】


[root@localhost test]# cat test.txt 

a

abc

ABCD

1

1234

abcd1234

12789Aabcdfepomz


1.^ 匹配行首

[root@localhost test]# grep -n '^a' test.txt 

1:a

2:abc

6:abcd1234


2.$ 匹配行尾

[root@localhost test]# grep -n 'CD$' test.txt 

3:ABCD


3. [ ]

匹配 [ ]內的字符

注意下面的執行是錯誤的

[root@localhost test]# grep -n [ a-b 7-9 ] test.txt 

grep: Invalid regular expression

[root@localhost test]# grep -n '[ a-b 7-9 ]' test.txt 

1:a

2:abc

6:abcd1234

7:12789Aabcdfepomz



4.  .

匹配任意的字符

[root@localhost test]# grep -n '.a' test.txt 

7:12789Aabcdfepomz


5. *

緊跟一個單字符,表示匹配0個或者多個此字符


Screen Shot 2017-10-11 at 6.05.18 PM.png


6. \  

用來屏蔽元字符的特殊含義

可以在文本內增加自己喜歡的內容,進行測試

[root@localhost test]# grep -n '\$' test.txt 

9:$


7. \?

匹配前面的字符0此或者多次

Screen Shot 2017-10-11 at 6.09.59 PM.png


8.  \+

匹配前面的字符1此或者多次

[root@localhost test]# grep -n --color '3\+' test.txt 

5:1234

6:abcd1234


9.  X\{m\}

匹配字符X m 次

Screen Shot 2017-10-11 at 6.40.44 PM.png


10. X\{m,\}

匹配字符X 最少m次

[root@localhost test]# grep -n --color 'ab\{1,\}' test.txt 

同上


11.X\{m,n\

匹配字符X m-n

Screen Shot 2017-10-11 at 6.42.48 PM.png


12.\|

表示或的關係

Screen Shot 2017-10-11 at 6.44.41 PM.png


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章