How to replace replace 1 line with 2 lines using sed?

I have a very simple text file here with the following contents

Code:
    line1
    line2
    line3
    line4

I want to modify the contents via sed(or some other app) so it becomes

Code:
    line1
    line2
    #this line was added by sed
    line3
    line4

So I tried

sed -e "s/line2/line2\\n#this line was added by sed/" my-text-file-here.txt

but the output is

Code:
    line1
    line2\n#this line was added by sed
    line3
    line4

Any ideas on how to do it correctly? I'm using default shell. Thank you
 
getting an error

4enOJId.png
 
With printf and ed you can do

Code:
printf "%s\n" l 3 i "#this line was added by ed" . w | ed my-text-file-here.txt

But added text will be little different of course ;)
 
Code:
$ sed -e 's/\(line2\)/\1\
> #this line was added by sed/' file_with_lines
line1
line2
#this line was added by sed
line3
line4
 
Back
Top