How to form a sed expression containing escaped characters

sed

Given a sed expression (and GNU sed 4.2.2 on ArchLinux)

/match/i\tline1\n\tline2

which should insert two tab-indented lines above the match, I find that the escaping of the first character (in the example, \t) is ignored but all other escaped characters are treated correctly.

Testing this like this:

echo match | sed -e '/match/i\tline1\n\tline2'

results in

tline1
    line2
match

It doesn't matter what the initial escaped character is (e.g. tab or newline) the result is the same. What is the correct way to consrtuct the expression so that the first character is treated correctly?

Best Answer

Check the gnu sed manual (http://www.gnu.org/software/sed/manual/html_node/Other-Commands.html#Other-Commands) -- the i command is actually the i\ command, so you just need an extra backslash

echo match | sed -e '/match/i\\tline1\n\tline2'
# ---------------------------^
Related Question