Combine multiple unix commands into one output

Another way is

{ grep ...; bzgrep ...;} >file

&& has the difficulty that the bzgrep wouldn't be run if the grep failed.

Note the mandatory space after the opening curly brace and semicolon after the last command. Alternatively, you can use the subshell syntax (parentheses instead of curly braces), which isn't as picky:

(grep ...; bzgrep ...) >file

bzgrep automatically defaults to regular grep if a file isn't bzip-compressed. Thus the following should be sufficient:

bzgrep [email protected] maillog *bz2 | mail -s "logs yay" someuser@blah

oh also of course here's my obligatory GNU Parallel solution too:

parallel -m bzgrep [email protected] ::: maillog* *bz2 | mail -s "logs yay" someuser@blah

which could be a lot faster if you are checking a lot of files.


Here's another way to do it (assuming you're running bash, which you probably are):

cat <(bzgrep ...) <(grep ...)

Here bash is transparently feeding the output of the bzgrep and grep commands into cat as if they were files (and they sort of are under the hood, details in url at the bottom).

In your particular case I'd recommend Phil's solution, but the above is a good trick to keep in your bag.

If you're interested, you can read more here: http://www.tldp.org/LDP/abs/html/process-sub.html