[sf-perl] Using the bistable .. op and regexps

yary not.com at gmail.com
Thu Sep 16 10:38:47 PDT 2010


Hi all,

I'm going through logs seeing if a change improved a particular SQL
statements execution time. The part I'm interested looks like this:

 insert into foo..bar_440_5678_D
 select
       a.pd,
       a.tg,
 ....
 Elapsed time: 0m27.485s

I'm interested in the "5678" and the time it took. Seems like the flip-flip
".." is perfect for a quick hack to extract that info:

perl -ne 'if(/insert into foo\.\.bar_440_(\d+)/../time: (.+)/&&$1){print
"$1\n"}' SQL.log

The thing is, it prints "5678" many times, until it gets to the "Elapsed
time" portion. I would expect that the lines between the start and the end,
which don't match, to clear $1.

I ended up with a longer one-liner to extract the info I was looking for-

perl -ne 'if(((/insert into foo\.\.bar_440_(\d+)/ and $a=$1)..(/time: (.+)/
and $b=$1))&&$b){print "$a took $b\n";$b=0}' SQL.log

Now I'm curious why I couldn't rely on $1 being undefined for the
intermediate lines. Anyone have a good explanation?

-y
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20100916/5b7fd099/attachment.html>


More information about the SanFrancisco-pm mailing list