[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: [PERL] Søg og erstat -> Hurtigt hack ønskes!



René Seindal wrote:

> Peter Toft wrote:
> >
> > perl -i -pe 's/andeby/gåserød/gi'
> > er en sød lille ordre til at erstatte alle andeby med gåserød.
> >
> > Men hvad gør jeg midt i et Perl program, hvis jeg har
> > filnavn : $filename
> > frastreng : $froms
> > tilstreng : $tos
> >
> > og vil erstatte alle $froms med $tos i $filename
> > resultatet skal også efterlades i $filename.
>
> system("sed -e 's/$froms/$tos/g' < $filename >$filename+ && mv
> $filename+ $filename");

Det må da være din spøg! Du kunne da i det mindste kalde perl, og lade være
med at bruge shell'en, altså:

     system("perl", "-pi", "-e", "s/$froms/$tos/g", $filename);

Mere oplagt ville nok være (gør precis det samme - men uden fork):

     open(IN, $filename) or die;
     open(OUT, ">$filename.tmp") or die;
     while(<IN>) {
         s/$froms/$tos/g;
         print OUT;
     }
     close(OUT);
     close(IN);
     unlink $filename;
     rename "$filename.tmp", $filename;

./borup



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 19:52 CEST [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] *