The following command will replace find with replace in all files ending in .pl in the current directory:
$ perl -p -i -e 's/find/replace/g' *.pl
*.pl can be replaced with a filename if you want to limit the changes to only one file.
$ perl -p -i -e 's/find/replace/g' file.pl
It’s permissible to put the the
-i options together rather than specifying multiple dashes. The following is the same as the preceeding example:
$ perl -pi -e 's/find/replace/g' file.pl
-e needs it’s dash. Specifying
-pie won’t work. Which is sad. Because everybody likes pie.
-i options specifies an in-place edit, which means file.pl will be modified. If you want to keep the original, omit the
-i and redirect the output to a new file:
$ perl -p -e 's/find/replace/g' file.pl > new-file.pl
Everything between the single quotes following the
-e is a regular expression, so things such as
^ (beginning of line) and
$ (end of line) are available. See perlre for more information on regular expressions.
You can combine multiple
-e options on the same line by simply ending each with a semicolon (;) inside the closing single quote. Note that the last
-e option doesn’t need a semicolon but it doesn’t hurt if you add it:
$ perl -e 's/replace/find/g;' -e 's/test/practice/g' file.pl