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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章