grep 命令执行什么?

What is a grep command performs?

我试图理解这个 unix 命令,但我不是这方面的专家,有人可以更详细地解释一下吗?

grep '^.\{167\}02'

它有什么作用?

找到从任意 (.) 167 个符号开始 (^) 且后跟 02 的行。

来自手册页 (man grep)

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

检查粗体部分:如果您没有指定要搜索的文件,它只会等待并听取您的键盘输入并对您键入的每个新行进行正则表达式匹配。

如果你想测试它,我建议你使用一个更简单的正则表达式,也许像这样的字符更少:^.\{3\}02 看看会发生什么:

$ grep '^.\{3\}02'
02
002
0002
00002 <-- this matches and will later be printed and highlighted
00002

您通常不会使用 grep 并自己键入行来查看是否匹配,而是将文件作为参数或使用管道的另一个输入:

ls -la | grep '^.\{167\}02'