Gossamer Forum
Home : General : Perl Programming :

Replacing Text in a File..

Quote Reply
Replacing Text in a File..
I am making a program that replaces any occurence of a certain piece of text.

ok.. this is what does work.. it doesn't have the "REPLACING" part.. cause i can't remember how to replace text.

foreach $line (@script) {
if ($line =~ /big/)
print $line;
}
elsif ($line =~ /heavy/)
print $line;
else {
print $line;
}
}

now all i need to know is how to replace the text.. in this example i would like to figure out how to change big to small and heavy to light...

any ideas?

i think it is something like this:

foreach $line (@script) {
if ($line =~ /big/small/)
print $line;
}
elsif ($line =~ /heavy/light/)
print $line;
else {
print $line;
}
}

but that doesn't work... help!

thanks!
Quote Reply
Re: Replacing Text in a File.. In reply to
You can use:

perl -p -i -e "s/search-for/replace-with/;" *.html

which would replace search-for with replace-with in every html file in the directory.

Hope this helps!

Alex