perl -i.bak -pe 's/pattern/replacement/'
If, however, you have decided to save your script in a file rather than just using a -e switch, then you might prefer to use the $^I special variable over the -i switch:
#!/usr/bin/perl
$^I = ".bak";
while (<>) {
s/pattern/replacement/;
print;
}
That way you won't forget that you have to supply the -i and clobber a file the next time you use the script. It also allows you to make the script itself executable without losing functionality. Putting it on the shebang line does too but this approach is better in that it is more flexible. For instance, you might not want to clobber file.bak if it exists but instead write to file.bak.N (where N is a sequence number) instead. Setting $^I in the script enables you to these kind of things.
-sauoq
"My two cents aren't worth a dime.";
|