开发者

concatenating linux program outputs, and returning only those which repeat

开发者 https://www.devze.com 2023-02-06 06:54 出处:网络
I have multiple programs which each produce lines of output. How do I concatenate those outputs, and then only return one copy of each line which repeated at least once? In other words, I want to retu

I have multiple programs which each produce lines of output. How do I concatenate those outputs, and then only return one copy of each line which repeated at least once? In other words, I want to return the set intersection of all response lines.

for example:

$ progA
9
13
14
15
$ progA --someFlag
13
14
15
100
$ progB
14
15
-42
$ magicFunction 'progA' 'progA --someFlag' 'progB开发者_StackOverflow社区'
14
15

This doesn't have to be a function per se. I just wanted a unix command-line way.


How about:

( progA; progA --someFlag; progB ) | sort | uniq -d

The -d option for uniq forces it to output only lines with more than one copy.

Here's a variant of the one-liner above that does not use a subshell:

{ progA; progA --someFlag; progB; } | sort | uniq -d

This works at least in bash. Note the required terminating semicolon (;) after the last command in the curly braces.


The solutions above don't really compute the set intersection of all 3 outputs. uniq -d will also output lines which are output only by 2 of the 3 programs. Here's my take on it:

progA | sort > f1; progA --someFlag | sort > f2; progB | sort > f3; comm -1 -2 f1 f2 | comm -1 -2 f3 -; rm f[123]

0

精彩评论

暂无评论...
验证码 换一张
取 消