Perl: Loop through a file and substitute

这一生的挚爱 提交于 2019-11-30 05:18:45

问题


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?


回答1:


I normally code up a one liner for this:

perl -i -pe 's/some/thing/' log.file

See Here




回答2:


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>



回答3:


or you can use sed (I know... you asked about perl):

sed -i 's/find/replace/g' <file>


来源:https://stackoverflow.com/questions/934733/perl-loop-through-a-file-and-substitute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!