How to replace all the blanks within square brackets with an underscore using sed?

Posted by Ringerrr on Stack Overflow See other posts from Stack Overflow or by Ringerrr
Published on 2010-12-21T20:21:59Z Indexed on 2010/12/21 21:54 UTC
Read the original article Hit count: 165

Filed under:
|
|

I figured out that in order to turn [some name] into [some_name] I need to use the following expression:

s/\(\[[^ ]*\) /\1_/

i.e. create a backreference capture for anything that starts with a literal '[' that contains any number of non space characters, followed by a space, to be replaced with the non space characters followed by an underscore. What I don't know yet though is how to alter this expression so it works for ALL underscores within the braces e.g. [a few words] into [a_few_words].

I sense that I'm close, but am just missing a chunk of knowledge that will unlock the key to making this thing work an infinite number of times within the constraints of the first set of []s contained in a line (of SQL Server DDL in this case).

Any suggestions gratefully received....

© Stack Overflow or respective owner

Related posts about regex

Related posts about sed