[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [perl] while løkke til variabel



I sslug.programmering, skrev Magnus Pedersen:
>  Følgende piller de linier ud af en logfil der svare til et søgekriterie:
>  #!/usr/bin/perl
> 
>  #Åben logfilen eller dø.
>  open LOG, '/var/log/squid/access.log' or die "Logfilen er ikke 
>  tilgængelig, er du Root?\n $! \n";
> 
>  ##Hvad skal vi søge efter
>  print "Hvilket udtryk vil du søge efter? \n";
>  my $search = <STDIN>;
>  chomp ($search);
> 
>  ##Hiv de linier ud der indeholder vores søgestreng
> 
>  while (<LOG>) {
> 
>  print if /$search/i;
> 
>  }
> 
>  Jeg har to spørgsmål, jeg er helt ny udi perl, er det den smarteste 
>  måde at gøre dette på?
> 
>  I tilfælde af jeg vil arbejde mere med de hits jeg får kan jeg så 
>  hive de "fangede" linier over i en variabel før/efter/samtidig med 
>  de bliver printet?

Ovenstående kunne omskrives til:

while(<LOG>){
   print if $_ =~ /$search/i;
}

Du kan rode med $_ så har du indholdet af linien. Smukkere er det dog
at:

while(my $line = <LOG>){


}

Og husk

use strict;
use warnings;

som de første 2 linier efter #!

Jesper
-- 
./Jesper Krogh, sslug@sslug, Jabber ID: sslug@sslug



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-02-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *