in reply to Look Ahead regexp to insert line?
#!/usr/bin/perl use warnings; use strict; open my $FH, '<', 'test.cfg' or die $!; my $pos; while (<$FH>) { $pos = $. if /echo.*install_list$/; # Remember the line numb +er. } die "Pattern not found.\n" unless $pos; $. = 0; # Reset the line number. seek $FH, 0, 0; # Start reading from the + beginning again. open my $OUT, '>', 'test.cfg.new' or die $!; while (<$FH>) { print {$OUT} $_; print {$OUT} "test\n" if $. == $pos; # After the remembered l +ine, print the extra text. } close $OUT; rename 'test.cfg.new', 'test.cfg';
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Look Ahead regexp to insert line?
by bowei_99 (Friar) on Oct 02, 2012 at 00:37 UTC | |
by choroba (Cardinal) on Oct 02, 2012 at 00:54 UTC |