search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[Linux] grep 指令使用方法

此篇文章瀏覽量: 15,335

此篇文章同步於 字媒體 部落格。

一、搜尋某檔案,找出”要搜尋的字串”的那行顯示出來:

$ grep "要搜尋的字串" 檔名

例如:

$ grep "name" composer.json composer.lock

或也可以使用萬用字元*,來搜尋多個檔案:

$ grep "name" *.md

 

二、使用 -r 來遞迴搜尋某資料夾裡的所有檔案,包含子資料夾,例如以下是搜尋當前目錄及其底下子資料夾裡的所有檔案:

$ grep -r "要搜尋的字串" .

 

三、find 指令找到當前目錄底下(包含子資料夾),檔名以 jsx 結尾的檔案,列出來:

$ find . -name "*jsx"

 

四、找出所有 .jsx 結尾的檔名,再 pipe 給後面的指令,透過 xargs 指令(eXtended ARGuments)搭配 grep 指令,找出那些檔名中,有出現 carlos 的各行:

$ find . -name "*.jsx" | xargs grep "carlos"
// 下方這行,與上方這行的結果會是一樣的,只是方式不同:
$ grep -r --include="*.jsx" "carlos" .

 

五、將所搜尋的字串,標出顏色:

$ grep -r --color uivacation .
// 如果是使用 git grep,可以排除被git忽略的檔案,如下:
$ git grep uivacation

 

六、也將搜尋到該行的行號( -n )給顯示出來:

$ grep -n -r --color uivacation resources/

 

七、將所搜尋到的那行,連同下一行也顯示出來(-A 1),如果要下兩行(-A 2),A代表的是 After;相反的,B是Before;C代表的是 Context,表示前後都會顯示:

$ grep -A 1 -n -r --color uivacation resources/
// -B 表示 Before,連同前一行也顯示出來
$ grep -B 1 -n -r --color uivacation resources/
// -C 表示 Context,連同前一行、後一行也顯示出來
$ grep -C 1 -n -r --color uivacation resources/

 

八、使用正規式,來找出某檔案的某字串:

// 下方的 "h." 是表示搜尋出 h 開頭,以及下一個字元是任意字元的字串:
$ grep --color -n "h." composer.json
// 使用 \ 來跳脫字元,以下例來說,就是一定要搜尋到 h. 才算符合:
$ grep --color -n "h\." composer.json
// 以下代表要搜尋到至少一個 # 字號,才算符合,因為後面的 #* 表示該 # 至少出現零次以上:
$ grep --color "##*" readme.md
// 以下代表在括號之間,可出現任意字元零次以上:
$ grep --color "(.*)" readme.md

 

九、使用進階的正規式:

// 在 readme.md 檔案中,搜尋出有 https 的所有行,並標示出顏色:
$ grep --color "https" readme.md
// 在 readme.md 檔案中,搜尋出有 http 開頭,後面搭配一個任意字元的所有行,並標示出顏色:
$ grep --color "http." readme.md
// 在 readme.md 檔案中,搜尋出有 http 開頭,後面的 s 可出現零次或一次,並標示出顏色:
$ grep --color "https\?" readme.md
// 在 readme.md 檔案中,搜尋出有 http 開頭,後面的 s 至少要出現一次以上,並標示出顏色:
$ grep --color "https\+" readme.md
// 如果不想加 \ 的話,那麼可改用 -E,例如上面這行,可改成以下這行,會是一樣的結果:
$ grep --color -E "https+" readme.md
// 下面這行,表示要搜尋的真的是 https+ 這樣的字串(也就是將 -E 移除):
$ grep --color "https+" readme.md

 

十、使用 | 這個代表或的運算

// 下面這個例子,會將 grey 及 gray 都標示出顏色:
$ echo "is it grey or gray?" | grep --color "grey\|gray"
// 或用以下語法也是可以的:
$ echo "is it grey or gray?" | grep --color -E "grey|gray"
// 以下例子為在 examples 資料夾底下(含子資料夾),搜尋出有 grey 或 gray 的所有行:
$ grep --color -rE "grey|gray" examples/

 

十一、使用 ^ 及 $ ,來表示那行的開頭跟結尾

// 表示在 app-spec.md 檔案中,要搜尋出以 # 字號開頭的行:
$ grep --color "^#" app-spec.md
// 表示在 app-spec.md 檔案中,要搜尋出以 , 字元結尾的行:
$ grep --color ",$" app-spec.md
// 表示在 examples 資料夾中(含子資料夾)的所有檔案,要搜尋出以 import 開頭,中間含有 from 的所有行:
$ grep --color -r "^import .* from" examples/

 

十二、使用 bracket 的表達式:[a-zA-Z]、[0-9]

// 以下是表示 a 和 b 會被搜尋出來並標示出顏色:
$ echo abc123 | grep --color "[ab]"
// 以下是表示 a 到 z 會被搜尋出來並標示出顏色:
$ echo abc123 | grep --color "[a-z]"
// 以下是表示 1 到 9 會被搜尋出來並標示出顏色:
$ echo abc123 | grep --color "[1-9]"
// 以下是表示 「de開頭,然後中間 a 到 z 的字元可任意出現多次,然後以 er 結尾」 會被搜尋出來並標示出顏色:
$ grep --color "de[a-z]*er" readme.md
// 以上的 [a-z] 可用以下方式來替代:
$ grep --color "de[[:alpha:]]*er" readme.md

$ find . -name "*.js" | grep --color "[sS]pec"

 

十三、group 例子:

// 找出 grey' 和 gray' 的所有行:
$ grep -rE --color "(grey|gray)\'" .
// 找出 grey' 和 gray'、grey"、gray" 的所有行:
$ grep -rE --color "(grey|gray)(\'|\")" .

 

十四、使用 find 找出所有的檔案列表,再透過 grep 將 node_modules 資料夾裡的排除

$ find examples/angularjs -name "*js" | grep -v "node_modules"

 

若覺得文章有幫助,請多分享,讓其他同好也能吸收網站技能知識。



熱門推薦

本文由 carlos-studiocom 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦