General discussion

Locked

insert new line using SED

By stevelil ·
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.

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by Nico Baggus In reply to insert new line using SED

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 = <IN&gt {
if ($line =~ /($match)/) {
# here you can do something you
needed to extract...
print $1,"\n";
}
print $line;
}
close IN;

Collapse -

by stevelil In reply to
Collapse -

by stevelil In reply to insert new line using SED

This question was closed by the author

Back to Linux Forum
3 total posts (Page 1 of 1)  

Related Forums