0

I am trying to replace _ with : in a file on a Unix system using sed:

sed "s/'_''/:/g"

However, it doesn't work. I cannot find a solution in similar sed related posts.

AdminBee
  • 22,803

1 Answers1

3

You have an extra single quote, and, in fact, don't need the quotes at all. The underscore and the colon are not significant to the shell. You are asking sed to match '_'' (all four characters) and replace them with a colon.

$ echo "_This is a test_" | sed s/_/:/g  
:This is a test:

Not that quotes aren't a good idea...

AdminBee
  • 22,803
  • 2
    Also, more efficient: sed 'y/_/:/', or even tr '_' ':' (or just tr _ : if you want to save on typing). – Kusalananda Aug 01 '21 at 20:10
  • The double quotes are not necessary, the stream presented to sed in the example is the same in any case. I would agree that if there were shell metacharacters in the stream, like a glob, you'd have a problem, but then add a comment explaining it. – Marc Wilson Aug 01 '21 at 22:33