亚洲成A人片在线观看网站_成年网站免费视频A在线双飞_日日日日做夜夜夜夜无码_久久夜色撩人精品国产小说

Grep 備忘清單

本備忘單旨在快速提醒使用命令行程序 grep 所涉及的主要概念,并假設您已經了解其用法。

入門

使用

搜索標準輸出(即文本流)

$ grep [options] search_string

在文件中搜索確切的字符串:

$ grep [options] search_string path/to/file

打印 myfile.txt 中包含字符串“mellon”的行

$ grep 'mellon' myfile.txt

文件名中接受通配符。

選項示例

選項示例說明
-igrep -i ^DA demo.txt忘記區分大小寫
-wgrep -w "of" demo.txt僅搜索完整的單詞
-Agrep -A 3 'Exception' error.log匹配字符串后顯示 3 行
-Bgrep -B 4 'Exception' error.log在匹配字符串前顯示 4 行
-Cgrep -C 5 'Exception' error.log在匹配字符串周圍顯示 5 行
-rgrep -r 'github.io' /var/log/nginx/遞歸搜索 (在子目錄內)
-vgrep -v 'warning' /var/log/syslog返回所有與模式不匹配的行
-egrep -e '^al' filename使用正則表達式 (以'al'開頭的行)
-Egrep -E 'ja(s|cks)on' filename擴展正則表達式 (包含 jason 或 jackson 的行)
-cgrep -c 'error' /var/log/syslog計算匹配數
-lgrep -l 'robot' /var/log/*打印匹配文件的名稱
-ogrep -o search_string filename只顯示字符串的匹配部分
-ngrep -n "go" demo.txt顯示匹配的行號

Grep 正則表達式

參考

有關更復雜的要求,請參閱完整版的正則表達式備忘單。

通配符(Wildcards)

:-:-
.任何字符
?可選且只能出現一次
*可選的,可以多次出現
+必需并且可以多次出現

量詞(Quantifiers)

:-:-
{n}前一項恰好出現 n 次
{n,}上一個項目出現 n 次或更多
{,m}上一個項目最多出現 n 次
{n,m}上一項出現在 n 到 m 次之間

POSIX

:-:-
[:alpha:]任何大小寫字母
[:digit:]任何數字
[:alnum:]任何大小寫字母或數字
[:space:]任何空格

字符串

:-:-
[A-Z-a-z]任何大小寫字母
[0-9]任何數字
[0-9-A-Z-a-z]任何大小寫字母或數字

位置

:-:-
^行的開頭
$行結束
^$空行
\<詞的開頭
\>詞尾

更多示例

搜索命令行歷史記錄

history | grep git

輸入過 git 命令的記錄

搜索多個文件并查找匹配文本在哪些文件中

grep -l "text" file1 file2 file3...

多級目錄中對文本進行遞歸搜索

grep "text" . -r -n

. 表示當前目錄。

搜索結果中包括或者排除指定文件

# 目錄中所有的 .php 和 .html 文件中
# 遞歸搜索字符 "main()"
grep "main()" . -r --include *.{php,html}

# 在搜索結果中排除所有 README 文件
grep "main()" . -r --exclude "README"

# 在搜索結果中排除 filelist 文件列表里的文件
grep "main()" . -r --exclude-from filelist

輸出包含匹配字符串的行數 -n 選項

grep "text" -n file_name
# 或
cat file_name | grep "text" -n

#多個文件
grep "text" -n file_1 file_2

忽略匹配樣式中的字符大小寫

echo "hello world" | grep -i "HELLO"
# hello

統計文件或文本中包含匹配字符串的行數 -c 選項

grep -c "text" file_name

另見

  • (jaywcjlove.github.io)