Perl: Loop through a file and substitute

前端 未结 3 473
攒了一身酷
攒了一身酷 2021-01-05 18:21

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?

相关标签:
3条回答
  • 2021-01-05 18:29

    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>
    
    0 讨论(0)
  • 2021-01-05 18:30

    I normally code up a one liner for this:

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

    See Here

    0 讨论(0)
  • 2021-01-05 18:45

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

    sed -i 's/find/replace/g' <file>
    
    0 讨论(0)
提交回复
热议问题