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

前端 未结 4 1942
迷失自我
迷失自我 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:36

    $_='~s/blue/red/g';

    Uh, what??

    Just

    s/blue/red/g;
    

    or, if you insist on using a variable (which is not necessary when using $_, but I just want to show the right syntax):

    $_ =~ s/blue/red/g;
    

提交回复
热议问题