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

后端 未结 3 645
北恋
北恋 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:25

    I would suggest another approach where file is processed line by line, and lines are modified within user specified $edit function.

    use strict;
    use warnings;
    
    sub edit_file {
      my $func = shift;
    
      # perl magic for inline edit
      local @ARGV = @_;
      local $^I = "";
      local $_;
    
      while (<>) {
        $func->(eof(ARGV));
      }
    }
    
    
    my $edit = sub {
      my ($eof) = @_;
    
      # print to editing file
      print "[change] $_";
      if ($eof) {
        print "adding one or more line to the end of file\n";
      }
    };
    edit_file($edit, "file");
    
    0 讨论(0)
  • 2021-01-19 08:28

    You can use the module File::Slurp to read, write, append, edit the lines, insert new lines in the file and many other things.

    http://search.cpan.org/~uri/File-Slurp-9999.19/lib/File/Slurp.pm

    use strict;
    use warnings;
    use File::Slurp 'write_file', ':edit';
    
    my $file = './test.txt';
    
    #The lines you want to change with their corresponding values in the hash:
    my %to_edit_line = ( edit1 => "new edit 1", edit2 => "new edit 2" );
    
    foreach my $line ( keys %to_edit_line ) {
        edit_file_lines { s/^\Q$line\E$/$to_edit_line{$line}/ } $file;
    }
    
    #The lines after you want to add a new line:
    my %to_add_line = ( add1 => 'new1', add2 => 'new2' );
    
    foreach my $line ( keys %to_add_line ) {
        edit_file_lines { s/^\Q$line\E$/$line\n$to_add_line{$line}/ } $file;
    }
    
    #The lines you want to delete:
    my %to_delete_line = ( del1 => 1, del2 => 1 );
    
    foreach my $line ( keys %to_delete_line ) {
        edit_file_lines { $_ = '' if /^\Q$line\E$/ } $file;
    }
    
    #You can also use this module to append to a file:
    write_file $file, {append => 1}, "the line you want to append";
    
    The original file test.txt had the following content:
    
    zzz
    add1
    zzz
    del1
    zzz
    edit1
    zzz
    add2
    zzz
    del2
    zzz
    edit2
    zzz
    
    After running the program, the same file has the following content:
    
    zzz
    add1
    new1
    zzz
    zzz
    new edit 1
    zzz
    add2
    new2
    zzz
    zzz
    new edit 2
    zzz
    the line you want to append
    
    0 讨论(0)
  • 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`;
    
    0 讨论(0)
提交回复
热议问题