Unix/Linux中的grep命令

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

null
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.显示匹配数的计数: 我们可以找到与给定字符串/模式匹配的行数

$grep -c "unix" geekfile.txt

输出:

2

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

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

输出:

geekfile.txt

4.检查文件中的所有单词: 默认情况下,grep匹配给定的字符串/模式,即使它是作为文件中的子字符串找到的。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

输出:

unixunixunixunixunixunix

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.匹配以字符串开头的行: “^regular expression”模式指定行的开头。这可以在grep中用于匹配以给定字符串或模式开头的行。

$ grep "^unix" geekfile.txt

输出:

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

9.匹配以字符串结尾的行: $regular expression模式指定一行的结尾。这可以在grep中用于匹配以给定字符串或模式结尾的行。

$ grep "os$" geekfile.txt

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

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

11.-f文件选项从文件中提取模式,每行一个。

$cat pattern.txtAgarwalAggarwalAgrawal

$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.

本文由 阿克谢·拉吉普特 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks的主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享