Perl: Loop through a file and substitute
-
06-09-2019 - |
Question
I simply wanna read in a logfile, do a search and replace, and then write out the changes to that same logfile.
What's the best practice way of doing this in Perl?
Solution
OTHER TIPS
This is often done with a one-liner:
perl -pi.bak -e "s/find/replace/g" <file>
Note the -i.bak
portion -- this creates a backup file with the extension .bak
. If you want to play without a net you can do this to overwrite the existing file without a backup:
perl -pi -e "s/find/replace/g" <file>
or you can use sed
(I know... you asked about perl):
sed -i 's/find/replace/g' <file>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow