1

I try to replace "_" to "/"

$ echo "hahahaa_the_cat_hahahaa" | sed "s/_///g"

It doesn't work.

Arronical
  • 19,893

3 Answers3

5

The substitution operator (s/old/new/) can take any character as a delimiter:

$ echo foo | sed 's|f|g|'
goo
$ echo foo | sed 'safaga'
goo

So just use anything that isn't / and you can do what you want:

$ echo "hahahaa_the_cat_hahahaa" | sed 's|_|/|g'
hahahaa/the/cat/hahahaa

Alternatively, you can escape the / with a \ (write \/):

$ echo "hahahaa_the_cat_hahahaa" | sed 's/_/\//g'
hahahaa/the/cat/hahahaa
terdon
  • 100,812
3

The problem is that you're using / as your delimiter in sed while also using it as the character to substitute.

Try using a different delimiter such as |:

$ echo "hahahaa_the_cat_hahahaa" | sed 's|_|/|g'
Arronical
  • 19,893
3

Escape the slash / character by backslash \:

$ sed 's/_/\//g' <(echo 'https:__askubuntu.com')
https://askubuntu.com

ie. using a \ before the "/" stops the "/" being seen as a seperator, but as a character to be translated... (this probably isn't worded very well sorry)

pa4080
  • 29,831
guiverc
  • 30,396