[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Sheflug] replacing strings in multiple files.
Barrie Bremner" <baz [at] barriebremner.com> mused:
> > perl -wpi.bak -e 's/old email/new email/g' *
>
> for i in *.html;
> do perl -wpi.bak -e 's/oldusr\@olddomain/newusr\ [at] newdomain/g' $i;
> done
>
> Seems to do the trick. Backups an' all!
>
> I need to learn perl.
> It was clever even enough to tell me to escape the [at] 's - I like that.
>
> I know nothing of the syntax - but you mention a while loop - the $i I
> assume.
>
> I am right in saying * is for all cases whereas $i would be once for
> each html file?
What's happening here is all to do with the options passed to perl.
The -w option gives warnings (hence warning you that you needed to
escape @'s)
The -p is a very useful shortcut saying: open and read in all the files
on the command line, then run the code on each line in turn, printing
the (potentially modified) line out after you've done with it.
The -i is even more magical - it says 'edit inline', ie for each file
that you're processing lines on, write the output lines back to the
same file (well, to a temporary file then rename after you've finished)
-i.suffix says first copy each old file to oldfile.suffix before stomping
on it.
-e says the code to execute is all containined in the next argument.
The * or *.html is just a wildcard that is expanded by the shell into
a list of files that's passed to the perl script to do it's -p and -i magic on.
s/patten/replacement/g says (s)ubstitute (g)lobally - ie as many times as
you can, replacement for pattern.
Learn Perl - it will change you life ;-)
Dave "I love Perl" M
---------------------------------------------------------------------
Sheffield Linux User's Group - http://www.sheflug.co.uk
To unsubscribe from this list send mail to
- <sheflug-request [at] vuw.ac.nz> - with the word
"unsubscribe" in the body of the message.
GNU the choice of a complete generation.