#!/usr/bin/perl
use strict;
use warnings;
my @text = ;
my $multipleinserts = 0;
my $alreadyseen = 0;
for (my $i = 0 ; $i < $. ; $i++) {
print $text[$i];
if ($text[$i] =~ /pattern to insert after/ && $multipleinserts || !$alreadyseen) {
print 1+$. .": new line here\n";
$alreadyseen = 1;
}
}
If the search patter appears twice then the text gets inserted after the first, or you can edit the obviously named variable to have it inserted after every occurrence. You didn't state what format the numbers take so I guessed. It shouldn't be too hard to change to suit your requirements. One caveat - it doesn't check for duplicate index values.
I don't know awk well enough so have an over-engineered perl solution instead
Date: 2009-05-15 09:31 pm (UTC)If the search patter appears twice then the text gets inserted after the first, or you can edit the obviously named variable to have it inserted after every occurrence. You didn't state what format the numbers take so I guessed. It shouldn't be too hard to change to suit your requirements. One caveat - it doesn't check for duplicate index values.