0

I need to replace a word "/hotel to "/Tay

I used various delimiters like ? $ but I couldn't get it

  • Do you need to use sed? Parameter expansion can do this: FOO='abc"/hoteldef'; echo ${FOO/"\/hotel/"\/Tay} – cprn Nov 17 '15 at 18:30
  • Since the characters "/ exist in both 'words', why not only change hotel to Tay using FOO='abc"\hoteldef'; echo ${FOO/hotel/Tay}? – Lambert Nov 17 '15 at 20:26
  • If any of the existing answers solves your problem, please consider accepting it via the checkmark. Thank you! – Jeff Schaller Apr 23 '17 at 12:51
1

People usually use forward slash / as the sed delimiter, but you can use any character you want; for example, !. Ideally, use a delimiter that doesn't appear in your search or replacement text, otherwise you'd need to quote it.

$ echo '"/hotel' | sed s!^"/hotel!"/Tay!
"/Tay

I quoted:

"hotel

with single-quotes to keep the shell from getting confused about the lone double-quote mark. Here's an unquoted version (that thus has to escape the double-quote marks from the shell):

$ echo \"/hotel | sed s!^"/hotel!"/Tay!

Edited to include the ^ "beginning of line" character to enforce the match at the beginning of the line. Example of a non-match:

$ echo foo\"/hotel | sed s!^"/hotel!"/Tay!
foo"/hotel
  • It should be enough using sed "s/\"\/hotel/\"\/Tay/g" – Rui F Ribeiro Nov 17 '15 at 18:32
  • 1
    indeed that works; it gets into the "leaning toothpick" problem, though, which is why I suggested changing the delimiter. – Jeff Schaller Nov 17 '15 at 18:37
-1

One way to do this is to escape the / and " with a "\"

echo '"/hotel' | sed 's/\"/hotel/Tay/g'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.