grep常見引數及應用舉例

2021-09-21 04:43:41 字數 4356 閱讀 8002

grep,是一種強大的文字搜尋工具,英文全稱為(global search regular expression(re) and print out the line),意為全面搜尋正規表示式並把行列印出來

。它能使用正規表示式搜尋文字,並把匹配的行列印出來。

【選項】 1

2

3

4

5

6

7

8

9

10

11

12

13

14

-c 計算找到"搜尋字串"的次數

-o 僅顯示出匹配regexp的內容(用於統計出現在文中的次數)

-i 忽略大小寫

-n 在行首顯示行號

-v 反向選擇,即顯示不包含"搜尋字串"內容的那一行

-e 擴充套件的grep,即egrep

--color=auto 以特定顏色高亮顯示匹配出的關鍵的

-a after的意思,顯示出匹配字串機器後n行的資料

-b before的意思,顯示匹配字串及其前n行的資料

-c 顯示匹配字串機器前後各n行

-r 遞迴搜尋

-e 制動多個匹配樣式

-q 不顯示任何資訊。

-w 只顯示全字符合的列。

【file檔案內容】 

1

2

3

4

5

6

7

[root@test1 test]# cat file

1

2 the world is not you can do, but you should.

3 life is not easy for any of us.

4 we must work hard.12345678

5 we hope that we can see you soon

6

【常見用法舉例】

1.排除指定內容

1

2

3

4

5

6

[root@test1 test]# grep -v we file

1

2 the world is not you can do, but you should.

3 life is not easy for any of us.

5 we hope that we can see you soon

6

2.統計we出現的次數

1

2

[root@test1 test]# grep -c we file

1

3.統計單詞"we"出現的次數,不區分大小寫

1

2

[root@test1 test]# grep -ci we file

2

4.僅輸出匹配到的內容

[root@test1 test]# grep -oi life file 

life

5.在行首顯示行號

[root@test1 test]# grep -ni life file 

3:3 life is not easy for any of us.

6.使用正規表示式-e選項

[root@test1 test]# grep -e "[1-9]+" file         

12 the world is not you can do, but you should.

3 life is not easy for any of us.

4 we must work hard.12345678

5 we hope that we can see you soon

6 7.匹配多個樣式

法一:利用egrep

[root@test1 test]# egrep -i --color=auto "we|life" file 

3 life is not easy for any of us.

4 we must work hard.12345678

5 we hope that we can see you soon

法二:利用-e選項

[root@test1 test]# grep -ie "we" -ie "life" file  

3 life is not easy for any of us.

4 we must work hard.12345678

5 we hope that we can see you soon

8.-q靜默輸出

1

2

3

[root@test1 test]# grep -q we file ;echo $?

0

#不會輸出任何資訊,如果命令執行成功返回0,失敗則返回非0值。一般用於條件測試。

9.列印出匹配文字之前或者之後的行

1)顯示匹配某個結果之後的3行,使用 -a 選項

[root@test1 test]# grep "3" -a 3 file 

3 life is not easy for any of us.

4 we must work hard.12345678

5 we hope that we can see you soon

6 2)顯示匹配某個結果之前的3行,使用 -b選項

[root@test1 test]# grep "5" -b 3 file 

12 the world is not you can do, but you should.

3 life is not easy for any of us.

4 we must work hard.12345678

5 we hope that we can see you soon

可以看到,輸出中多了第一行,可使用-w選項精確匹配字元,只輸出第五行及其前三行,即2-5行。

[root@test1 test]# grep -w "5" -b 3 file    

2 the world is not you can do, but you should.

3 life is not easy for any of us.

4 we must work hard.12345678

5 we hope that we can see you soon

3)顯示匹配某個結果的前三行和後三行,使用 -c 選項

[root@test1 test]# grep -w "4" -c 2 file 

2 the world is not you can do, but you should.

3 life is not easy for any of us.

4 we must work hard.12345678

5 we hope that we can see you soon

6

LCD屏引數及應用舉例

1.lcd引數及原理 r g b 訊號 pclk 畫素時鐘 lclk hsync,線時鐘,水平同步時鐘 fclk vsync,幀時鐘,垂直同步時鐘 7寸屏一般由兩種工作模式de和時鐘模式,一般都採用時鐘模式。4.3寸以下 包含3.5寸屏 與7寸屏不同,需要通過spi介面初始化。時序 800 480 ...

volatile作用及應用舉例

保證執行緒可見性 不能代替synchronized 保證原子性 禁止指令重排序 細節與cpu有關雙重判斷單例 要不要加volatile 需要 一定需要 防止指令重排 synchronized 不能防止重排序 public class singletonclass return instance pr...

grep命令常用引數及用法

grep命令是linux系統中一種強大的文字搜尋工具,它能使用正規表示式搜尋文字,並把匹 配的行列印出來。grep全稱global regular expr ession print,表示全域性正規表示式版本,它的使用許可權是所有使用者。grep可用於shell指令碼,因為grep通過返回乙個狀態值...