theravadamonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a file /tmp/file1.txt. it is like this.

Message Time: Feb 3 11:24:54

Mail From: <letter@leemaletter.com>

Mail to: <info@example.com>,<jay@example.com>

quarantine: spam-JF6LBytcy_3D.gz

Message Time: Feb 3 11:25:18

Mail From: <noreply@lankaemarketing40.info>

Mail to: <jay@example.com>

quarantine: spam-uXWM3SJ-nlB0.gz

Message Time: Feb 3 11:25:29

Mail From: <bounce@rhmarketing.info>

Mail to: <sam@example.com>

quarantine: spam-uOG8ycqZrq1s.gz

Message Time: Feb 3 11:25:47

Mail From: <jay@example.com>

Mail to: <userx@gmail.com>

quarantine: spam-FxfQDEDm-5MW.gz

Now, I want to get the output of user jay's info.

OUTPUT shoud be like this.

Message Time: Feb 3 11:24:54

Mail From: <letter@leemaletter.com>

Mail to: <info@example.com>,<jay@example.com>

quarantine: spam-JF6LBytcy_3D.gz

Message Time: Feb 3 11:25:18

Mail From: <noreply@lankaemarketing40.info>

Mail to: <jay@example.com>

quarantine: spam-uXWM3SJ-nlB0.gz

Message Time: Feb 3 11:25:47

Mail From: <jay@example.com>

Mail to: <userx@gmail.com>

quarantine: spam-FxfQDEDm-5MW.gz

How can I achieve this?

I have written a code like this. But it does NOT work?. I also need a case insensitive search

open(my $fh_daily_user_in, '<', $qfn_daily_user_in) or die("Unable to +read file \"$qfn_daily_user_in\": $!\n"); while (<$fh_daily_user_in>) { if (/jay/) { print $_ , "\n"; } }

any comment?

Replies are listed 'Best First'.
Re: search a string and get the output that belongs to the string
by Athanasius (Archbishop) on May 07, 2016 at 02:53 UTC

    Hello theravadamonk,

    It’s simpler if you read in a whole message (four lines: Message Time, Mail From, Mail to, quarantine) at a time, and then search for “jay” in one of the addresses. Here is one approach:

    #! perl use strict; use warnings; use Data::Dump; my @message; READ_FILE: while (<DATA>) { push @message, $_; for (1 .. 3) { last READ_FILE unless defined(my $line = <DATA>); push @message, $line; } if ($message[1] =~ /<jay@/i || # Mail From: $message[2] =~ /<jay@/i) # Mail to: { print "\n", join('', @message); } @message = (); } __DATA__ Message Time: Feb 3 11:24:54 Mail From: <letter@leemaletter.com> Mail to: <info@example.com>,<jay@example.com> quarantine: spam-JF6LBytcy_3D.gz Message Time: Feb 3 11:25:18 Mail From: <noreply@lankaemarketing40.info> Mail to: <jay@example.com> quarantine: spam-uXWM3SJ-nlB0.gz Message Time: Feb 3 11:25:29 Mail From: <bounce@rhmarketing.info> Mail to: <sam@example.com> quarantine: spam-uOG8ycqZrq1s.gz Message Time: Feb 3 11:25:47 Mail From: <Jay@example.com> Mail to: <userx@gmail.com> quarantine: spam-FxfQDEDm-5MW.gz

    Output:

    12:51 >perl 1624_SoPW.pl Message Time: Feb 3 11:24:54 Mail From: <letter@leemaletter.com> Mail to: <info@example.com>,<jay@example.com> quarantine: spam-JF6LBytcy_3D.gz Message Time: Feb 3 11:25:18 Mail From: <noreply@lankaemarketing40.info> Mail to: <jay@example.com> quarantine: spam-uXWM3SJ-nlB0.gz Message Time: Feb 3 11:25:47 Mail From: <Jay@example.com> Mail to: <userx@gmail.com> quarantine: spam-FxfQDEDm-5MW.gz 12:51 >

    Notes:

    1. Case-insensitive matching is achieved by adding the /i modifier to the regex.
    2. The above approach will work only if the 4-line messages are placed contiguously in the data file (no intervening blank lines), with the first message beginning on the first line.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks. Works very well. Sorry for the delay in writing. I was very busy. Pls forgive me.
Re: search a string and get the output that belongs to the string
by graff (Chancellor) on May 07, 2016 at 20:17 UTC
    Here's another way (very similar to the first reply, but a little simpler):
    #!/usr/bin/env perl use strict; use warnings; my @message; while (<DATA>) { push @message, $_; if ( /quarantine/ ) { # this is the last expected line of each me +ssage local $_ = join( "", @message ); local $\ = "\n"; print if ( /<jay@/i ); @message = (); } }
Re: search a string and get the output that belongs to the string
by NetWallah (Canon) on May 08, 2016 at 01:53 UTC
    You can take advantage of the fact that your email messages seem to be separated by a blank line (presumably \n\n).

    You can write a one-liner like :

    perl -ne 'BEGIN{$/=qq|\n\n|}print if m/jay/s' Your-quarantine-file-nam +e-here.txt

            This is not an optical illusion, it just looks like one.