Apparently, though unproven, at 13:25 on Saturday 12 February 2011, 
meino.cra...@gmx.de did opine thusly:

> Hi,
> 
>  I am trying to instruct sed to insert a line of text before
>  a matched line. The whole command should fit into one
>  physical (command) line.
> 
>  Is it possible? And how is it possible?
> 
>  Thank you very much for any hint in advance!
>  Best regards,
>  mcc


There's nothing special about a line, it's just a bunch of characters that end 
with a newline (itself just a character).

But you can't insert stuff at arbitrary points, you can only replace stuff 
with other stuff. You can replace the start of line marker (^), so do this:

$ cat sed.txt 
1
2
$ cat sed.txt | sed -e 's/^/a\n/g'
a
1
a
2

I replaced "start of line" with "a and a newline". Modify the regex to suit 
your needs. This gets awkward though, as you can search with a regex but only 
replace a literal. If you need to insert some line before any line containing 
say a "z" for example, then that is way beyond sed's capabilities and you are 
into awk|perl territory.

You didn't clearly state what you are trying to do with examples, so the above 
vague wishy-washy goop is the best I can do for you.


-- 
alan dot mckinnon at gmail dot com

Reply via email to