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

时间:2021-11-27 21:45:36

I want to filter the output of arbitrary output e.g. cat or objdump to only display lines which contain "pattern".

我想过滤任意输出的输出,例如cat或objdump,只显示包含“模式”的行。

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

是否有一行UNIX/Linux命令可以做到这一点?

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

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

2 个解决方案

#1


13  

cat file | grep pattern

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

如果是静态文件,也可以使用grep模式文件。

#2


7  

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

最好使用grep -e或鹭(这允许扩展正则表达式)。然后你可以用regex做更健壮的事情:

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

如果您使用-o,则只有返回的数字(而不是前面和后面的内容)。

#1


13  

cat file | grep pattern

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

如果是静态文件,也可以使用grep模式文件。

#2


7  

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

最好使用grep -e或鹭(这允许扩展正则表达式)。然后你可以用regex做更健壮的事情:

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

如果您使用-o,则只有返回的数字(而不是前面和后面的内容)。