[Chicago-talk] /i resets $1 on failed matches?

Andy_Bach at wiwb.uscourts.gov Andy_Bach at wiwb.uscourts.gov
Wed Jul 14 11:14:58 CDT 2004

Hi folks,

[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 
>> 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.

> This is the answer that was 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 
> Without it, the script produced:

> with it, it produced:

[end other list quotes]

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_bach at 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 

More information about the Chicago-talk mailing list