in reply to Saving Results of a while loop
This incorporates all the relevant features and fixes mentioned above. The quotemeta escapes the . chars in the regex which would otherwise match anything. We use $_ in the while loop that iterates over our logfile as this a normal perl idiom. This lets us use m/blah/ as this is the same as $_ =~ m/blah/. We can compile once using /o so don't need to use qr. By not chomping off the newlines in the first place you don't need to put them back again, so print @fwlog is all you need as the newlines are still there.
my $ip = "192.168.3.5"; $ip = quotemeta $ip; my @fwlog; my $logfile = "/tmp/05jul01.drop"; open LOG, "$logfile" or die "Can't open $logfile: $!\n"; while (<LOG>){ push @fwlog,$_ if m/\b$ip\b/o; } close LOG; print @fwlog;
Hope this helps
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|