sed - find&replace - in multiple files

I have 60+ *.xhtml files

I have to find particular phrase (e.g. <h3 id=“top”>) which includes spaces, <, “, etc.
and replace it with another phrase
(e.g. <a class"top” href="…/file/file.xhtml>), also with spaces and other ‘characters’.

I tried to use sed:

find . -name "*.xhtml" | xargs sed -i 's/<h3 id="top">/<h3 id="top"><a class="tyt" href="">/g'

and have error:

sed: -e expression #1, char 54: unknown option to `s'


find . -name "*.xhtml" | xargs sed -i 's/'<h3 id="top">'/'<h3 id="top"><a class="tyt" href="">'/g'

with this error

syntax error near unexpected token `&lt;'


find . -name "*.xhtml" | xargs sed -i 's/"<h3 id="top">"/"<h3 id="top"><a class="tyt" href="">"/g'

with error

sed: -e expression #1, char 57: unknown option to `s'

What shall I do?
Any proper way of entering this command?
Any help?
Or how to do that?


I discovered that problem is “backslash /” in the expression.

Yes and no.

It certainly is the / (it is a “slash” not a “backslash”). And you can avoid that by using another character instead of all three / that are relevant to the s command. One that does not appear anywhere in both strings. The % for example. Remember that the fisrt character after the s defines what you should use to delimiter both strings.

I think he meant


but, like me, saw that that looks like a capital v.

I can not find the sequence / anywhere.

His first example is wrong because / is used as the string delimiter for the sed s command. and there are /s all over the place.

I did not try to evaluate the second example because it is even weirder

Like in escaping a space char in a file name on a bash shell: file\ name.

I agree that using a \ before characters in the REGEXP and/or the REPLACEMENT that are the same as the delimiter character, should help also (but I still think using a different delimiter is easier to type and read), but why does he then say:

I discovered that problem is “backslash /” in the expression.

without btw showing he problem.

I solved the problem using , (comma) as a delimiter. Then I could f&r whatever I wanted (fortunately there were no commas in my expression :wink: )

Some explanations:
I had problem with / (slash) because it was used as a delimiter and character in my expression - that’s why it caused problems.
I didn’t know (prior) that first letter after 's is delimiter. Most examples are with / as a delimiter. Thanks to hcvv and his explanation on delimiter, plus few examples on the net, I tried to use comma and it worked!

So for me for now the problem is solved.
Thanks everyone.

That is fine. I often use % because that is even less used in many cases then , .

And yes, the / is allmost allways ussed (and is more or less automaticly used by my fingers when I am using vi), but one should llways use the documentation (hehe).