How to add and replace lines in a line array with perl

后端 未结 3 649
北恋
北恋 2021-01-19 08:11

I want to edit a file by adding some line and replacing some others. I\'m trying to work with an array that contains my file line by line, i.e

    my $outpu         


        
3条回答
  •  终归单人心
    2021-01-19 08:30

    Use splice to alter the contents of @LINES.

    Use open and print to write @LINES back to your file.

    If other people might be editing this file at the same time then you'll need flock.

    If performance isn't that important to you then you might look at Tie::File.

    For more complicated file handling, you might want seek and truncate.

    But this is all covered well in the Perl FAQ - How do I change, delete, or insert a line in a file, or append to the beginning of a file?

    By the way, your first two lines of code can be replaced with one:

    my @LINES = `cat $result_dir/$file`;
    

提交回复
热议问题