[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!



Christian Borup wrote:
> 
> 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!

Hvorfor skulle det være en spøg? Det er netop en af unix's stærke sider,
at man så let kan bygge på andre programmer.

At undlade det er blot et tegn på at man ikke har forstået unix-ideen
fuldt ud.

> 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);

Jeg tror, at en sh og sed starter hurtigere end perl, der er et ret
stort program. Jeg kan ikke se at denne løsning skulle være bedre, med
mindre man da elsker perl næsten religiøst og ser alle andre programmer
som inferiøre.

> 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;

Vi snakkede om et hurtigt hack, ikke om at genopfinde hjulet for n'te
gang. Hvorfor programmere den funktion igen, når den er lavet så mange
gange. Genbrug er en af kerne-ideerne i unix.


-- 
René Seindal (sslug@sslug)			http://www.seindal.dk/rene/


 
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] *