1. 程式人生 > >grep(1) 正則表示式文字查詢工具

grep(1) 正則表示式文字查詢工具

還沒寫完!!!

1. 概念 CONCEPT

1. 什麼是匹配器 Matcher

匹配器就是查詢文字所用的正則表示式的種類。好比用 google 搜尋還是用 baidu 搜尋。

2. 什麼是匹配控制 Matching Control

匹配控制就是匹配的規則,如何匹配。匹配哪個或哪些樣式,匹配樣式來自輸入還是來自檔案,匹配樣式是否忽略大小寫,獲取滿足匹配的文字還是獲取不滿足匹配的文字,按單詞匹配或者按行匹配。

3. 什麼是輸出控制 Output Control

正常情況下,輸出列印的是滿足選項和樣式的行。但是,我們有時候不需要顯示每個行,或者滿足條件的行太多,我們只需要統計滿足的行數即可。或者我們查詢的是多個檔案,不必列印每個檔案的所有匹配行,只需要知道哪些檔案符合匹配,打印出匹配的檔名即可。或者我們只關心匹配的前幾行。或者我們想忽略掉錯誤資訊的輸出。因此不再顯示正常的輸出,而是輸出我們指定的內容。

4. 什麼是上下文行 Context Line

有時我們不只關心匹配的那一行,還關心匹配行的前幾行或後幾行,例如我找到一個段 的標題行,我還要知道這個標題下面的幾行內容的情況。

2. 語法 SYNOPSIS

兩種型別的語法:

  1. 選項 輸入樣式 目標檔案 目標檔案
  2. 選項 輸入樣式 輸入樣式 檔案樣式 檔案樣式 目標檔案 目標檔案

一定要看清除規律,區分樣式和目標檔案。[-f FILE] 指的是樣式,而不是要查詢的檔案。

注意 grep 可以進行多檔案查詢。

grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN]...
[-f FILE]... [FILE...]

3. 描述 DESCRIPTION

grep searches the named input FILEs for lines containing a match to the given PATTERN. If no files are specified, or if the file “-” is given, grep searches standard input. By default, grep prints the matching lines.

grep 搜尋 FILEs 檔案並獲取匹配 PATTERN 的行。如果沒有指定檔案或檔案以”-“形式給出,則搜尋標準輸入。預設下,grep 列印匹配的行。

In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated, but are provided for backward compatibility.

另外,變體程式 egrep, fgrep and rgrep 分別同 grep -E, grep -F, and grep -r. 不建議使用這些變體,但是為了向後相容性,依然會提供。

4. 選項 OPTIONS

1. 通用程式資訊 Generic Program Information

1. –help

Output a usage message and exit.

2. -V, –version

Output the version number of grep and exit.

2. 匹配器選擇 Matcher Selection

匹配器有基本正則表示式匹配,擴充套件正則表示式匹配,Perl 正則表示式匹配和普通固定文字匹配。

預設情況下,輸入的樣式被當作基本正則表示式處理,即 -G 選項預設開啟,如果不想使用正則表示式,可以使用 -F 選項。

1. -E, –extended-regexp

Interpret PATTERN as an extended regular expression (ERE, see below).

將 PATTERN 解釋為擴充套件的正則表示式。

2. -F, –fixed-strings

Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.

將 PATTERN 解釋為固定的字串的列表(禁用正則表示式),使用換行符分離,每個字串都會被匹配。

3. -G, –basic-regexp

Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.

將 PATTERN 解釋為基礎的正則表示式。這是預設的。

4. -P, –perl-regexp

Interpret PATTERN as a Perl regular expression (PCRE, see below). This is highly experimental and grep -P may warn of unimplemented features.

將 PATTERN 解釋為 Perl 正則表示式。這是高度實驗性的選項,可能會警告為未生效的特性。

3. 匹配控制 Matching Control

1. -e PATTERN, –regexp=PATTERN

Use PATTERN as the pattern. Multiple -e can be used to specify different search patterns. This option is also useful to protect a pattern beginning with a hyphen (-).

預設情況下,命令後面接一個 PATTERN,如果想使用多個 PATTERN,則可以使用多個 -e 選項指定不同的搜尋樣式。匹配到的樣式都會被打印出來。

這個選項也可以用於保護以連字元號打頭的樣式。即以連字元號開頭的檔案不會被誤認為是引數。

2. -f FILE, –file=FILE

Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. Multiple -f can be used to
specify different files.

從檔案獲得樣式,每個樣式佔一行。空檔案不包含樣式因此無法匹配。多個-f選項可以用於指定多個檔案。
即可以把樣式儲存在檔案中,方便日後使用。

3. -i, –ignore-case

Ignore case distinctions in both the PATTERN and the input files.

輸入樣式和樣式檔案均忽略大小寫。

4. -v, –invert-match

Invert the sense of matching, to select non-matching lines.

顛倒匹配的識別,用於選擇不匹配指定樣式的行。

5. -w, –word-regexp

Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-
word constituent character. Word-constituent characters are letters, digits, and the underscore.

僅選擇匹配整個單詞的行。
匹配子字串的測試必須要麼在行的開始,要麼在非列印字元前面。
類似的,它必須要麼在行的末尾,要麼跟在非列印字元的後面。
可列印字元包括字母,數字和下劃線。

6. -x, –line-regexp

Select only those matches that exactly match the whole line. For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $.

僅選擇匹配整個行的行。
對於正則表示式樣式來說,這就像給樣式加上括號然後用^和$圍起來。

7. -y

Obsolete synonym for -i.

舊式的等同於 -i 的選項。

4. 通用輸出控制 General Output Control

1. -c, –count

Suppress normal output; instead print a count of matching lines for each input file. With the -v, –invert-match option (see below), count non-matching lines.

阻止正常的輸出,而是列印匹配每個輸入檔案的行的個數。

使用 -v 選項則對非匹配行計數。

2. –color[=WHEN]

Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS. The deprecated environment variable GREP_COLOR is still supported, but its setting does not have priority. WHEN is never, always, or auto.

使用轉義序列包圍匹配的非空字串,匹配行,上下文行,檔名,行號,位元組偏移和分隔符,來在終端上用顏色顯示他們。顏色由 GREP_COLORS 環境變數定義。
不被推薦使用的 GREP_COLOR 環境變數依然被支援,但是他的設定不具有優先順序。

3. -L, –files-without-match

Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.

阻止正常輸出,而是列印每個正常情況下不會列印輸出的輸入檔案的名字。
找到第一個匹配則停止掃描。

即在找到需要的匹配前,列印掃描到的檔案的名字,以告訴使用者掃描正在進行。

4. -l, –files-with-matches

Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will
stop on the first match.

阻止正常輸出,而是列印每個正常情況下會列印輸出的輸入檔案的名字。

找到第一個匹配則停止掃描。

即找到第一個符合匹配的檔案,並列印這個檔名。

5. -m NUM, –max-count=NUM

Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep
ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing
context lines. This enables a calling process to resume a search. When grep stops after NUM matching lines, it outputs any trailing
context lines. When the -c or –count option is also used, grep does not output a count greater than NUM. When the -v or –invert-match
option is also used, grep stops after outputting NUM non-matching lines.

只讀取前 NUM 個匹配的行。

6. -o, –only-matching

Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

只打印匹配行的匹配部分。每個匹配部分佔一個輸出行。

我猜應該是適合一行中有多個匹配的情況。

7. -q, –quiet, –silent

Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
Also see the -s or –no-messages option.

靜默模式,不向列印任何輸出。

8. -s, –no-messages

Suppress error messages about nonexistent or unreadable files.

不輸出不存在或不可讀檔案的相關的錯誤資訊。

5. 輸出行字首控制 Output Line Prefix Control

6. 上下文行控制 Context Line Control

1. -A NUM, –after-context=NUM

Print NUM lines of trailing context after matching lines. Places a line containing a group separator (–) between contiguous groups of
matches. With the -o or –only-matching option, this has no effect and a warning is given.

列印匹配行的下面 NUM 個行。

2. -B NUM, –before-context=NUM

Print NUM lines of leading context before matching lines. Places a line containing a group separator (–) between contiguous groups of
matches. With the -o or –only-matching option, this has no effect and a warning is given.

列印匹配行的上面 NUM 個行。

3. -C NUM, -NUM, –context=NUM

Print NUM lines of output context. Places a line containing a group separator (–) between contiguous groups of matches. With the -o or
–only-matching option, this has no effect and a warning is given.

列印匹配行的上面和下面各 NUM 個行。

7. 檔案和目錄選擇 File and Directory Selection

8. 其他選項 Other Options