(Shrug...) "It's just me, but" I'd use
awk for that.
An
awk "program" is really drop-dead simple: it consists of one or more blocks that look like this:
Code:
/pattern/
{
block of code to be executed if this pattern was matched,
written in a "vaguely 'C'-like" language.
}
There are also "pseudo-patterns," like
BEGIN (which executes before the first line is read), and
END.
"And that's about
it."
What I really
like about it is that it's obviously designed-for tasks just like the one you are facing. Furthermore, the solution remains easy-to-read and easy-to-
change.
I mean... yes, I could "figure out" that
sed-script that you've shown, and a few weeks from now I could "figure it out all over again," but
(a) I would indeed have to do that, and
(b) it would require about the same amount of mental-effort each time. And the same would be true if I had written it myself. Also,
(c) if the input format subsequently changed, however slightly, I fear that I would be having to figure-it-out all over
again!
I therefore find that
awk, and its "honorary big-brother"
perl (which is a full-fledged programming language), are much more suited to these common tasks.
As the Perl community likes to say,
TMTOWTDI =
"there's more than one way to do it." That's especially true in Unix/Linux. It's definitely worth your time to spend the time poking around your system.