How to replace a string in an existing file in Perl?

前端 未结 4 1957
迷失自我
迷失自我 2020-12-02 13:04

I want to replace word \"blue\" to \"red\" in all text files named as 1_classification.dat, 2_classification.dat and so on. I want to edit the same file so I tried this code

4条回答
  •  孤街浪徒
    2020-12-02 13:23

    It can be done using a single line:

    perl -pi.back -e 's/oldString/newString/g;' inputFileName
    

    Pay attention that oldString is processed as a Regular Expression.
    In case the string contains any of {}[]()^$.|*+? (The special characters for Regular Expression syntax) make sure to escape them unless you want it to be processed as a regular expression.
    Escaping it is done by \, so \[.

提交回复
热议问题