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

hi. i hope someone can help out a n00b here. i'm trying to create a command line script to grab 1 data record out of a log file. each entry in the log file starts out with something like this:
*** 2007-08-17 08:30:51 *** [UID] = 030318520070817083011 + followed by information, including line breaks and empty lines, rep +eat of the same 21-digit number, etc. *** 2007-08-17 09:25:17 *** [UID] = 030104220070817092407 + Reason = Web server error *** XML Request *** more data, line breaks, spaces, etc.
so i need everything up until the next entry. i was able to get it to work with the following perl script (the argument to the script is that 21-digit number)
$badxml = `cat /mmf/log/BadXMLXact*`; $badxml =~ /@ARGV[0](.|\n)+/; $& =~ m/\*\*\*.*UID/; $output = $`; for ($output ) { s/\s{3,}/\n/g; } print $output;
and now i need to get it to work as a single line command line script. I tried this, as well as many different variations but obviously without any luck:
perl -00 -nle 'print if /21-digit-number(.|\n)+(\*\*\*\.*UID)/'
your help is very much appreciated!

Replies are listed 'Best First'.
Re: onle liner help needed
by holli (Abbot) on Aug 17, 2007 at 18:10 UTC
    There is (mostly) no need for linebreaks in perl code, so the answer to your question is as simple as
    perl -e '$badxml = `cat /mmf/log/BadXMLXact*`; $badxml =~ /21-digit-n +umber(.|\n)+/; $& =~ m/\*\*\*.*UID/; $output = $`; for ($output ) { +s/\s{3,}/\n/g; } print $output;'
    Besides that, I wonder about the reason you need to do it as a one liner, when you have a working script? I mean, a one liner is cool but not an end in itself.


    holli, /regexed monk/
      great! that worked. appreciate it, holli. i needed the one-liner since i have to run it remotely on several servers and did not want to install the script on every one of them.
Re: onle liner help needed
by akho (Hermit) on Aug 17, 2007 at 16:37 UTC
    I may have misunderstood the task, but perl -ne 'print if (/030318520070817083011/ ... /\*\*\*.*UID/);' FILE seems about right.