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'm trying to search for an unknown string (Message ID) in a log based on a known string (recipient address), and then grep the unknown string to output the entire relevant log information. I'm able to use grep and cut to output the unknown string(s), but from there I'm stumped on passing that to grep. I've used a pipe to xargs grep and that is not producing the output I'm looking for. Here is the command I'm using to grep and cut:

grep 'to=<emailaddress' /var/log/maillog | cut -d ' ' -f 6

This produces output of all the unknown strings for messages processed by Postfix for emailaddress. When I pipe to xargs I receive "grep: : No such file or directory:

grep 'to=<emailaddress' /var/log/maillog | cut -d ' ' -f 6 | xargs grep /var/log/maillog

Thanks for your help.

share|improve this question
    
I'm a bit lost at the moment. What do you want? A list of the message IDs of the mails sent to a special recipient? –  Mario Lenz Aug 25 at 19:51
    
The grep utility can read the patterns to match from a file with the -f switch. You can also use - as the filename for -f so it will read stdin from the pipe. No need for xargs. –  Iain Aug 25 at 19:56
    
I am searching for all Message IDs where the pattern is 'to=<emailaddress', and then output all log information regarding each Message ID. Since I do not know what the Message ID is, nor any other relevant information other than the e-mail address, I needed to be able to find all Message IDs and output the relevant log information for each. Thanks Iain. That did the trick: grep 'to=<emailaddress' /var/log/maillog | cut -d ' ' -f 6 | grep -f - /var/log/maillog. –  uxphreak Aug 25 at 20:03

1 Answer 1

up vote 4 down vote accepted

I've been playing around a bit with my own mail logs. Try

grep 'to=<emailaddress' /var/log/maillog | cut -d ' ' -f 6 | grep -f - /var/log/maillog

The -f - will read the list of strings to search for from stdin.

share|improve this answer

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.