1

I have a string:

172.16.1.5] "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 172.16.1.5]:"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?

3
  • 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 '19 at 16:14
  • @suspectus I think there is a space at the end that is getting replaced
    – john doe
    Sep 16 '19 at 16:17
  • ok, I read "I need to replace all the spaces after the ] " with a colon" literally.
    – suspectus
    Sep 16 '19 at 16:22
3

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

1

you could use

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

to have

172.16.1.5] "80:EE:73:2F:0B:40

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.