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,


In reply to Re: search a string and get the output that belongs to the string by Athanasius
in thread search a string and get the output that belongs to the string by theravadamonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.