How do I add lines to the top and bottom of a file in Perl?

后端 未结 9 1136
自闭症患者
自闭症患者 2021-01-06 23:38

I want to add a line to top and bottom of the file. I can do it following way.

open (DATA, \"         


        
9条回答
  •  失恋的感觉
    2021-01-06 23:57

    I don't really speak Perl, but perhaps this works for some situations:

    perl -0777 -pi -e 's/^/MY TEXT TO PREPEND/' myfile.txt
    

    That is, open the file in paragraph mode (one line), and replace the start of that line with your new text, doing an in-place rewrite.

    Probably not efficient for many large files.

提交回复
热议问题