Welcome Guest!
 Previous Message All Messages Next Message 
/i resets $1 on failed matches?  Andy_-@wiwb.uscourts.gov
 Jul 11, 2004 20:47 PDT 

From a couple other lists we have the question:
 Does anyone know how I can get just the IP address out from lots of
below is just two of them, there is about 2000 like this. Is there a flash
grep which can be used?

bash-2.03$ cat b.txt
S<job jobID="77042" type="SNMPSet"><task taskID="1"
name="hostname"></attr><attr name="hostport">161</attr></dE
S<job jobID="90713" type="SNMPSet"><task taskID="1"

Just need the Ip address.

And an answer:

 This is the answer I sent in:
cat b.txt |
    perl -ne '/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/i;
             print "$1\n" if $1;'

Now my question.... why did I need to put the "i" at the end of the regex?
Without it, the script produced:

with it, it produced:

I pointed out that:
cat b.txt |
    perl -ne 'print "$1\n"
if /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;'

was a slightly "better" idea, as $1 wasn't being reset on a failed match
and so existed/causes the print. I can't find where/why the /i resets $1
on a failed matches though. The "Owls" book does talk about the expense
and copies made with '/i' and how it ends up w/ at least 2 REs (original
and a lower case version) but I don't have a debug version of perl to try
-Dr on to see the difference (assuming I could understand the output).

Anybody have an answer?


Andy Bach, Sys. Mangler
Internet: andy_-@wiwb.uscourts.gov
VOICE: (608) 261-5738 FAX 264-5932

Beware of bugs in the above code; I have only proved it correct, not tried
it. -- Donald Knuth
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Food & Drink
 Health & Fitness
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.