Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I need to search our mail logs for a specific e-mail address. We keep a current file named maillog as well as a week's worth of .bz2 files in the same folder. Currently, I'm running the following commands to search for the file:

grep person@domain.com maillog
bzgrep person@domain.com *.bz2

Is there a way combine the grep and bzgrep commands into a single output? That way, I could pipe the combined results to a single e-mail or a single file.

share|improve this question

4 Answers 4

up vote 18 down vote accepted

Another way is

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

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

share|improve this answer
    
+1 for the best general purpose shell approach to this issue. Shell grouping is a generally underutilized feature. –  Phil Hollenback Mar 8 '11 at 23:07
    
This is exactly what I was looking for. Thanks! –  Ben McCormack Mar 9 '11 at 15:19
1  
Don't you mean () instead of {}? –  Ehtesh Choudhury Dec 21 '12 at 22:28

You can tie the commands together with && which will allow you to run each command.

you could also add >> textfile.txt to the end of each command and have the output hit a file then mail that file out.

share|improve this answer
2  
As geekosaur mentioned, && shouldn't be used here because grep's return value depends on whether it found something or not. If you did grep ... && bzgrep ..., if grep had no hits, it would return failure and the command would stop. >> is a good idea though, unlike >, it will add output to the end of an existing file. –  DerfK Mar 8 '11 at 22:36
    
right I had forgotten about the exit condition preventing the second command. –  Mike Mar 8 '11 at 22:52

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

bzgrep person@domain.com maillog *bz2 | mail -s "logs yay" someuser@blah

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

parallel -m bzgrep person@domain.com ::: maillog* *bz2 | mail -s "logs yay" someuser@blah

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

share|improve this answer

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

share|improve this answer
    
Ah yes, the canonical answer for 'how do you diff the output of two processes'. A good trick to know. –  Phil Hollenback Mar 9 '11 at 3:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.