# sed replace misplaces slashes

I have the following code in a LaTeX file:

for \emph{service} in \emph{host}.resources:
for \emph{file} in \emph{host}.resources:
for \emph{package} in \emph{host}.resources:


Because this is inside a listing enviroment and I still want the emph and underline commands I escape the LaTeX commands, which results in something like this:

for %*\emph{file}*) in %*\emph{host}*).resources:


I tried to automate this process using the following sed command:

sed "s_$$\\emph{[a-z]*}$$_%\*\1\*)_"


But this results into the following output:

for \%*emph{dir}*) in \emph{host}.resources:


There are two problems with this:

1. The '\' before emph is placed before the LaTeX escaping characters.
2. sed only seems to replace the first match. I know about the greedy matching in sed, which is why I used [a-z]* instead of .* inside the curly braces. But this doesn't seem to work.

Any ideas on how to make sed do as I want?

-
You want to replace every occurrence of \emph{anything} with \%*emph{anything}*? – mikeserv May 25 '14 at 8:00
\emph{X} should become %*\emph{anything}*). – Foezjie May 25 '14 at 11:58

sed 's_$$\\emph{[a-z]*}$$_%\*\1\*)_g'

You shouldn't use the reference at all. s|\\emph{[^}]*}|%*&*|g is what you should do. – mikeserv May 25 '14 at 8:02