Perl - Insert lines after a match is found in a file

前端 未结 5 1680
南笙
南笙 2021-01-24 09:34

I have a file with the following syntax in some_1.xyz

module some_1 {
INPUT PINS
OUTPUT PINS
}

and I want to insert APPLY DELAYS xx and APPLY L

5条回答
  •  隐瞒了意图╮
    2021-01-24 10:22

    I have no idea why your code isn't working, but I have trouble following your use of Perl inside backticks inside Perl. This is untested, but should work. I suggest you also "use strict;" and "use warnings;".

    my @files = ("some_1.xyz", "some_2.xyz", ... );
    for my $file in ( @files )
    {
        my $outfile = $file + ".tmp";
        open( my $ins, "<", $file ) or die("can't open " . $file . " for reading: " . $!);
        open( my $outs, ">", $outfile ) 
            or die("can't open " . $outfile . " for writing: " . $!);
        while ( my $line = <$ins> )
        {
            print { $outs } $line;
            if ( $line =~ m/^module\s+/ )
            {
                 print { $outs } "\tAPPLY DELAY xx\n\tAPPLY LOADS ld\n";
            }
        }
        rename( $outfile, $file );
    }
    

提交回复
热议问题