bash 如何同时 grep 多行(来自另一个命令的输出)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11773888/
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-09-18 02:55:18  来源:igfitidea点击:

How do I grep multiple lines (output from another command) at the same time?

linuxbashscriptinggrep

提问by peonicles

I have a Linux driver running in the background that is able to return the current system data/stats. I view the data by running a console utility (let's call it dump-data) in a console. Alldata is dumped every time I run dump-data. The output of the utility is like below

我有一个在后台运行的 Linux 驱动程序,它能够返回当前的系统数据/统计信息。我通过在控制台中运行控制台实用程序(我们称之为dump-data)来查看数据。所有的数据转储每次运行时转储数据。该实用程序的输出如下所示

Output:
- A=reading1
- B=reading2
- C=reading3
- D=reading4
- E=reading5
...
- variableX=readingX
...

The list of readings returned by the utility can be really long. Depending on the scenario, certain readings would be useful while everything else would be useless.

该实用程序返回的读数列表可能非常长。根据场景的不同,某些读数会很有用,而其他一切都是无用的。

I need a way to grep only the useful readings whose names might have have nothing in common (via a bash script). I.e. Sometimes I'll need to collect A,D,E; and other times I'll need C,D,E.

我需要一种方法来仅 grep 名称可能没有共同点的有用读数(通过 bash 脚本)。即有时我需要收集 A、D、E;其他时候我需要 C、D、E。

I'm attempting to graph the readings over time to look for trends, so I can't run something like this:

我正在尝试绘制随时间变化的读数以寻找趋势,因此我无法运行这样的操作:

# forgive my pseudocode
Loop
    dump-data | grep A
    dump-data | grep D
    dump-data | grep E
End Loop

to collect A,D,E as that would actually give me readings from 3 separate calls of dump-data as that would not be accurate.

收集 A、D、E,因为这实际上会给我来自 3 个单独的转储数据调用的读数,因为这不准确。

回答by Igor Chubin

If you want to save all result of grep in the same file, you can just join all expressions in one:

如果要将 grep 的所有结果保存在同一个文件中,您可以将所有表达式合并为一个:

grep -E 'expr1|expr2|expr3'

But if you want to have results (for expr1, expr2 and expr3) in separate files, things are getting more interesting.

但是如果你想在单独的文件中得到结果(对于 expr1、expr2 和 expr3),事情就会变得更有趣。

You can do this using tee >(command).

您可以使用tee >(command).

For example, here I process the same pipe with thre different commands:

例如,在这里我使用三个不同的命令处理相同的管道:

$ echo abc | tee >(sed s/a/_a_/ > file1) | tee >(sed s/b/_b_/ > file2) | sed s/c/_c_/ > file3
$ grep "" file[123]
file1:_a_bc
file2:a_b_c
file3:ab_c_

But the command seems to be too complex.

但是命令似乎太复杂了。

I would better save dump-dataresults to a file and then grep it.

我最好将dump-data结果保存到一个文件中,然后 grep 它。

TEMP=$(mktemp /tmp/dump-data-XXXXXXXX)
dump-data > ${TEMP}
grep A ${TEMP}
grep B ${TEMP}
grep C ${TEMP}

回答by scai

You can use dump-data | grep -E "A|D|E". Note the -Eoption of grep. Alternatively you could use egrepwithout the -Eoption.

您可以使用dump-data | grep -E "A|D|E". 注意-Egrep的选项。或者,您可以在egrep没有-E选项的情况下使用。

回答by Vijay

you can simply use:

你可以简单地使用:

dump-data | grep -E 'A|D|E'

回答by Júnior Mendon?a

awk '/MY PATTERN/{print > "matches-"FILENAME;}' myfile{1,3}

thx Guruat Stack Exchange

THX大师堆栈交易所