perl -pi -e 's/old_var/new_var/g' filename
or generate a backup file:
perl -pi'.bak' -e 's/old_var/new_var/g' filename #(backup name is filename.bak)
perl -pi'*_another_name' -e 's/old_var/new_var/g'
The former one will get backup file named "filename.bak", the latter one is
"filename_another_name", which means (see perldoc perlrun for details):
If the extension doesn't contain a "*", then it is appended to the
end of the current filename as a suffix. If the extension does
contain one or more "*" characters, then each "*" is replaced with
the current filename. In Perl terms, you could think of this as:
Now, we met this problem: how to edit file within a perl script??
Normally, the equivalent codes with the above commnads are like:
end of the current filename as a suffix. If the extension does
contain one or more "*" characters, then each "*" is replaced with
the current filename. In Perl terms, you could think of this as:
Now, we met this problem: how to edit file within a perl script??
Normally, the equivalent codes with the above commnads are like:
|
Supposed the script name is 'test.pl', we run it like this:
perl test.pl filename
which will be equivalent with the above one line perl command.
To avoid giving input filename to the script, we simply add this line:
@ARGV = ("filename");
|
At last, I have to say that we don't need to do it in such a way, please
try this Perl module: . which is easy to use.