通过regexp过滤linux bash输出的每一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15579507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:31:40  来源:igfitidea点击:

filter each line of linux bash output by regexp

linuxbashunix

提问by T. Webster

I want to filter the output of arbitrary output e.g. cator objdumpto only display lines which contain "pattern".

我想过滤任意输出的输出,例如catobjdump仅显示包含“模式”的行。

Is there a one-liner UNIX/Linux command to do this?

是否有单行 UNIX/Linux 命令来执行此操作?

e.g. cat filepath | xargs grep 'pattern' -lis not working for me

例如cat filepath | xargs grep 'pattern' -l不适合我

采纳答案by Explosion Pills

cat file | grep pattern

You could also just use grep pattern fileif it's a static file.

grep pattern file如果它是静态文件,您也可以使用。

回答by Paul Calabro

Better to use grep -eor egrep(this allows for extended regular expressions). Then you can do more robust things with regex:

最好使用grep -eor egrep(这允许扩展正则表达式)。然后你可以用正则表达式做更强大的事情:

 cat my_phonebook | egrep "[0-9]{10}"

To show all 10 digit phone numbers in a file.

显示文件中的所有 10 位电话号码。

If you toss in a -o, only the numbers get returned (instead of the before and after content on the line).

如果您投入 a -o,则只会返回数字(而不是行上的前后内容)。