Parsing Through exim4’s mainlog with perl

This one-liner is really simple, but it illustrates some of the cool things you can do with perl as you approach the limitations of bash.

The purpose of this one-liner is to start with an email address and then pull out the log messages that relate to the email event.

grep <something> /var/log/exim4/mainlog | perl -e 'while(<>){/ ([0-9a-zA-Z-]*) =>/; print `grep $1 /var/log/exim4/mainlog`;}'

The first part is a simple grep where we are searching a file for the¬†occurrence¬†of a given string. We then pipe the output into perl. Once in perl we use a “while” loop operating on standard input(STDIN or <STDIN> or <>). We use a regular expression to capture the transaction id of the email which we then use in another grep to print the entire email’s logs to the screen. This makes more sense when the code is rearranged like so.

    / ([0-9a-zA-Z-]*) =>/;
    print `grep $1 /var/log/exim4/mainlog`;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s