📜  Unix/Linux 中的 grep 命令

📅  最后修改于: 2022-05-13 01:57:28.314000             🧑  作者: Mango

Unix/Linux 中的 grep 命令

grep 过滤器在文件中搜索特定的字符模式,并显示包含该模式的所有行。在文件中搜索的模式称为正则表达式(grep 代表全局搜索正则表达式并打印出来)。  
句法:

grep [options] pattern [files]

Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
 with each such part on a separate output line.

-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.

示例命令

将以下文件视为输入。



$cat > geekfile.txt

unix is great os. unix is opensource. unix is free os.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

1. 不区分大小写的搜索: -i 选项可以在给定文件中不区分大小写地搜索字符串。它匹配诸如“UNIX”、“Unix”、“unix”之类的词。

$grep -i "UNix" geekfile.txt

输出:

unix is great os. unix is opensource. unix is free os.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

2. 显示匹配次数:我们可以找到匹配给定字符串/pattern 的行数

$grep -c "unix" geekfile.txt

输出:

2

3. 显示与模式匹配的文件名:我们可以只显示包含给定字符串/pattern 的文件。

$grep -l "unix" *

or
 
$grep -l "unix" f1.txt f2.txt f3.xt f4.txt

输出:

geekfile.txt

4. 检查文件中的整个单词:默认情况下,grep 匹配给定的字符串/pattern,即使它是文件中的子字符串。 grep 的 -w 选项使其仅匹配整个单词。



$ grep -w "unix" geekfile.txt

输出:

unix is great os. unix is opensource. unix is free os.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

5. 仅显示匹配的模式:默认情况下,grep 显示具有匹配字符串的整行。我们可以使用 -o 选项使 grep 仅显示匹配的字符串。

$ grep -o "unix" geekfile.txt

输出:

unix
unix
unix
unix
unix
unix

6. 使用 grep -n 显示输出时显示行号:显示与行匹配的文件的行号。

$ grep -n "unix" geekfile.txt

输出:

1:unix is great os. unix is opensource. unix is free os.
4:uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

7. 反转模式匹配:您可以使用 -v 选项显示与指定搜索字符串模式不匹配的行。

$ grep -v "unix" geekfile.txt

输出:

learn operating system.
Unix linux which one you choose.

8. 匹配以字符串开头的行: ^ 正则表达式模式指定行的开头。这可以在 grep 中使用以匹配以给定字符串或模式开头的行。

$ grep "^unix" geekfile.txt

输出:

unix is great os. unix is opensource. unix is free os.

9. 匹配以字符串结尾的行: $ 正则表达式模式指定行的结尾。这可以在 grep 中使用以匹配以给定字符串或模式结尾的行。



$ grep "os$" geekfile.txt

10.用 -e 选项指定表达式。可以多次使用:

$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" geekfile.txt

11. -f file option 从文件中获取模式,每行一个。

$cat pattern.txt

Agarwal
Aggarwal
Agrawal

$grep –f pattern.txt  geekfile.txt

12. 从文件中打印 n 个特定行: -A 打印搜索到的行和结果后的 n 行,-B 打印搜索到的行和结果前的 n 行,-C 打印搜索到的行和结果后的 n 行。

句法:

$grep -A[NumberOfLines(n)] [search] [file]  

$grep -B[NumberOfLines(n)] [search] [file]  

$grep -C[NumberOfLines(n)] [search] [file]  

例子:

$grep -A1 learn geekfile.txt

输出:

learn operating system.    
Unix linux which one you choose. 
--
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.      

(Prints the searched line along with the next n lines (here n = 1 (A1).)
(Will print each and every occurrence of the found line, separating each output by --) 
(Output pattern remains the same for -B and -C respectively)                                                                                                                                       Unix linux which one you choose.                                                                                                                                            --                                                                                                                                                                          uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.                                                                                                                                                    Unix linux which one you choose.                                                                                                                                            --                                                                                                                                                                          uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.