{%hackmd j1-jtNWMSlW8Hpfz6P2p4g %} # grep ## 語法 ``` grep "match_pattern" [files...] [args] ``` ### 簡易範例 ```shell= ## 遍歷此資料夾底下所有檔案(包含子資料夾), 尋找符合 `abc` 的資料行 grep "abc" ./ -r ## 尋找此資料夾副檔名為 `.txt` 中符合 `abc` 的資料行 grep "abc" *.txt ``` ## 常用速查表 |args|用途|範例| |-|-|-| |`-n`|顯示行號|`grep "aaa" -n`| |`--color`|顏色highlight|`grep "aaa" --color=auto`| |`-v`|排除, 也就是反向搜尋, 只顯示不符合的項目|`grep "aaa" -v`| |`-r`|包含子目錄的所有目標|`grep "aaa" -r`| |`-A {N}`|多顯示後{N}行的內容|`grep "aaa" -A 1`| |`-B {N}`|多顯示前{N}行的內容|`grep "aaa" -B 2`| |`-C {N}`|多顯示前後各{N}行的內容|`grep "aaa" -C 3`| |`-E`|啟用正規表示法, 等同使用`egrep`|`grep "[0-9]+" -E`| |`-c`|統計符合條件的次數|`grep "aaa" -c`| |`-i`|忽略大小寫|`grep "aaa" -i`| |`-e`|增加`match_pattern`|`grep "aaa" -e "bbb" -e "ccc"`| ## 參考 - [Linux 匹配文字 grep 指令用法教學與範例](https://blog.gtwang.org/linux/linux-grep-command-tutorial-examples/) ###### tags: `grep` `linux`
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.