in reply to Re: usage of awk and grep together
in thread usage of awk and grep together

Hi, Thanks, but im very very new to perl. the fileid and the specific data in the code are also stored in an array. i pick them from an array store them in a variable and substitute them in the command. Also i am not familiar with shell scripting.

Replies are listed 'Best First'.
Re^3: usage of awk and grep together
by juster (Friar) on May 20, 2009 at 18:18 UTC

    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.