Discussion Locked

insert new line using SED

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

As part of a script, I need to:
Search a file for a string.
Extract part of that string.
Use that part string in a new line.
Insert that new line just before the original line.

3 replies

by nico baggus

I thing this is above en beyond the ability of
sed: but perl can help you here....

---8<---
#!perl -w
$match = $ARGV[0];
$file = $ARGV[1];
if (not defined $file) {
$file = "-";
}

open(IN,"<".$file) || die ("Cannot open file:
$file\n");
while ($line = ) {
if ($line =~ /($match)/) {
# here you can do something you
needed to extract...
print $1,"\n";
}
print $line;
}
close IN;

This discussion is closed to new replies.