I would suggest playing with creating subs and passing them arguments if you haven't already. Like this example you can take code you already have and put them in a nice friendly named container. This is a fundamental skill that is very handy.

Call the sub with just the $fileid and $specificdata that you want given as arguments.

You retrieve the arguments very easily:

sub get_log_data { my ($fileid, $match) = @_; #...

Then just replace fileid with $fileid and specificdata with $match.

As you can see there are many other ways to do this. I provided the longest way I could think of because I hoped it would be easier to understand and learn from. Short one-liners generally have many subtle concepts that make them work. i.e. here is the sub using johngg's approach, one I would use for a quick n dirty script.

sub get_log_data { my ($fileid, $match) = @_; local @ARGV = 'logfile'; return join '', grep { (/$fileid/o .. /\A-{5}/) && /$match/o } <>; }

I'd suggest using a long, explicit, easy to debug, easy to understand version for a beginner; as a learning exercise.


In reply to Re^3: usage of awk and grep together by juster
in thread usage of awk and grep together by raghu_shekar

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.