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.


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...

  • 2
    Also, more efficient: sed 'y/_/:/', or even tr '_' ':' (or just tr _ : if you want to save on typing).
    – Kusalananda
    Aug 1 '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. Aug 1 '21 at 22:33

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.