grep的簡單的用法

2021-07-11 15:01:45 字數 2140 閱讀 4936

演示檔案:

[root@cyf opt]# cat  123.txt 

qqqqqqqqqq

wwwwwwwww

eeeeeeeeeaa

rrrrrr

aaaaa

111222

3334

5a

1

-a表示匹配本行和之後的行

[root@cyf opt]# grep  -a 1 111 123.txt 

111222

2

-b表示匹配本行和之前的行

[root@cyf opt]# grep -b 1 111  123.txt 

aaaaa

111

3

-c表示 匹配本行與上下行

[root@cyf opt]# grep  -c 1 222 123.txt 

111222

333

4

-c表示grep的引數有多少個

[root@cyf opt]# grep  -c a  123.txt 

3

5

–color表示顏色顯示

[root@cyf opt]# grep  --color  11 123.txt 

111

6

-e後面跟正規表示式,可以跟多個正則

[root@cyf opt]# grep  -e  ^e  -e 2$  123.txt 

eeeeeeeeeaa

222

7

-f指定檔案內容作為查詢條件

[root@cyf opt]# cat we 

11[root@cyf opt]# ls

123.txt we

[root@cyf opt]# grep -f we 123.txt

111

8

-m 最多匹配多少個個數作為條件

[root@cyf opt]# cat  123.txt 

qqqqqqqqqq

wwwwwwwww

eeeeeeeeeaa

rrrrrr

aaaaa

111222

33345a

[root@cyf opt]# grep -m 2 a 123.txt

eeeeeeeeeaa

aaaaa

9

-n表示行數計數

[root@cyf opt]# grep -n a 123.txt 

3:eeeeeeeeeaa

5:aaaaa

11:a

10

-o 只列印匹配的內容

[root@cyf opt]# grep -o  11 123.txt 

11[root@cyf opt]# grep -o 1 123.txt 11

1

11

-r搜素子目錄內容

[root@cyf opt]# grep  -r a *

1/2/3/spur:aaaaa

123.txt:eeeeeeeeeaa

123.txt:aaaaa

123.txt:a

we:a

12

-v表示查詢不含引數的行

[root@cyf opt]# grep  -v a 123.txt 

qqqqqqqqqq

wwwwwwwww

rrrrrr

111222

3334

5

13

w查詢乙個詞

[root@cyf opt]# cat  wea12

3344

5566

77a s d f

[root@cyf opt]# grep -w 12 we

1233

44

grep過濾的用法

grep的工作方式是這樣的,它在乙個或多個檔案中搜尋字串模板。如果模板包括空格,則必須被引用,模板後的所有字串被看作檔名。搜尋的結果被送到螢幕,不影響原檔案內容。grep可用於shell指令碼,因為grep通過返回乙個狀態值來說明搜尋的狀態,如果模板搜尋成功,則返回0,如果搜尋不成功,則返回1,如果...

grep命令的用法

1.作用 linux系統中grep命令是一種強大的文字搜尋工具,它能使用正規表示式搜尋文字,並把匹 配的行列印出來。grep全稱是global regular expression print,表示全域性正規表示式版本,它的使用許可權是所有使用者。2.格式 grep options 3.主要引數 o...

grep命令的用法

grep命令的用法 grep global search regular re and print out the line,全面搜尋正規表示式並把行列印出來 是一種強大的文字搜尋工具,它能使用正規表示式搜尋文字,並把匹配的行列印 出來.引數 1.anum after context num 除了列...