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

following up from previous node I posted

I need also to grab each line starting with particular ip address and bung it in array

@ip = $log =~/192\.168\.1\.10/g;

Will grab the IP address but I need all the line...

Simple I bet, but I'm struggling with this one

update (broquaint): changed href to link

Replies are listed 'Best First'.
Re: and a simple reg exp poser
by diotalevi (Canon) on Jan 30, 2003 at 14:56 UTC

    Solved without a regex so it's even clearer

    if ( -1 != index $log, '198.167.1.10' ) { push @ip, $log; # or... print $log # or whatever makes sense for you } # OR just written differently push @ip, $log if -1 != index $log, '198.167.1.10'; print $log if -1 != index $log, '198.167.1.10'; # Or... as a regexp but I prefer it the other way push @ip, $log if $log =~ /198\.167\.1\.10/; print $log if $log =~ /198\.167\.1\.10/;

    Update Ok, maybe a regex is better because then then it can be written like /198\.167\.1\.10(?!\d)/ which would fix the problem Abigail-II pointed out.


    Seeking Green geeks in Minnesota

      That would capture lines containing 198.167.1.103.

      Abigail

Re: and a simple reg exp poser
by Cabrion (Friar) on Jan 30, 2003 at 15:11 UTC
    The suggestions so far don't really do what you said you wanted done.

    1) All lines starting with a particular IP.

    2) Assuming "bung"="add" that it's an additive thing, not an overwrite thing.

    my @ip while (<LOG>) { #assuming you are looping through a log file my $log = $_; #making this blatent push(@ip, $log) if $log =~ /^192\.168\.1\.10/;
    From your code, the 'g' modifier means match as many times as possibe.

    From mine, '^' is an anchor to the begining of the string.

    From your code, '@ip = $log' overwrites the values of $ip[0] each time.

    From my code, 'push(@ip, $log)' adds the entire line to the end of the array.

    Update: diotalevi's concept of using index instead of a regexp was an excellent suggestion that avoids regex engine overhead, so . . .

    push(@ip, $log) if 0 == index($log,'192.168.1.10');
    Which gives you those that start with the ip in question.
Re: and a simple reg exp poser
by Jaap (Curate) on Jan 30, 2003 at 14:51 UTC
    try this:
    @ip = $log =~/(192\.168\.1\.10.*?)\n/g;
    (not tested)

      stew asserted that the entire line should be captured. Your expression ignores everything on the line prior to the match. You want to just store the value of $log if it's a match, not do some fancy footwork with capturing. Its just buggy this way.


      Seeking Green geeks in Minnesota

      (tested)

      Thank you!

Re: and a simple reg exp poser
by robartes (Priest) on Jan 30, 2003 at 15:00 UTC
    How's this:
    #!/usr/local/bin/perl -w use strict; my $log="192.168.1.254 was around"; my $ip="192.168.1.254"; my @ip; push @ip,($log =~ /(^\Q$ip\E.*$)/); print @ip; __END__ 192.168.1.254 was around

    Update: Added ^ anchor, poster wanted IP at start of string.

    CU
    Robartes-