[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



Magnus Pedersen wrote (29-01-2006 22:01):
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?

Jeg har ikke testet følgende men det er umiddelbart mine reaktioner:

Brug /oi på det regulære udtryk i $search. Option 'o' betyder at det kun bliver oversat en gang i stedet for hver gang det udføres.

Du kan lave en

	@hits = grep { /$search/oi } <LOG>;

og så arbejde på @hits.

Din kode ville være

	print grep { /$search/oi } <LOG>;

hvis du ikke vil gemme de fundne linier.

Du glemmer at printe de newlines ud du fjerner med chomp.  Du skal nok bruge

	print $_, "\n"

da du ellers fjerne newlines fra resultaterne.


Husk at lukke LOG med mindre du exit'er umiddelbart efter.

--
René Seindal (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] *