I have a string:] "80 EE 73 2F 0B 40

I need to replace all the spaces after the ] " with a colon. Currently using sed -r 's/[ ]+/:/g' I get an output of]:"80:EE:73:2F:0B:40:, however I need to keep the first space after the bracket.

How can I replace all the instances of a character, but only after a match using sed?

  • end of string is 0B:40: after sed. Why the colon at end of string? There's no ] " at end of original string.
    – suspectus
    Sep 16, 2019 at 16:14
  • @suspectus I think there is a space at the end that is getting replaced
    – john doe
    Sep 16, 2019 at 16:17
  • ok, I read "I need to replace all the spaces after the ] " with a colon" literally.
    – suspectus
    Sep 16, 2019 at 16:22

2 Answers 2


There are different approaches possible.

A conditional loop, that replaces one space at a time:

sed -e :1 -e 's/\(\] .*\) /\1:/; t1'

Saving the prefix, remove it, substitution, restore prefix:

sed '/\] /{ s//&\
/; h; s/.*\n//; s/ /:/g; H; g; s/\n.*\n//;}'

Neither will do any substitution if the input doesn't contain "] ".


you could use

echo '] "80 EE 73 2F 0B 40' | sed -r 's/ +/:/g;s/\]:/\] /g'

to have] "80:EE:73:2F:0B:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .