Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3606140
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:12:42+00:00 2026-05-18T21:12:42+00:00

I figured out that in order to turn [some name] into [some_name] I need

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:12:43+00:00Added an answer on May 18, 2026 at 9:12 pm

    There are two parts to the trickery needed:

    1. Stop replacing when you reach a close square bracket (but do it repeatedly on the line):

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

      This matches an open square bracket, followed by zero or more characters that are neither a blank nor a close square bracket. The global suffix means that the pattern is applied to all sequences starting with an open square bracket followed eventually by a blank or close square bracket on the line. Note, too, that this regex does not alter ‘[single-word] and context‘ whereas the original would translate that to ‘[single-word]_and context‘, which is not the object of the exercise.

    2. Get sed to repeat the search from where this one started. Unfortunately, there isn’t a truly good way to do that. Sed always resumes searching after the text that was substituted; and this is one occasion when we don’t want that. Sometimes, you can get away with simply repeating the substitute operation. In this case, you have to repeat it every time the substitution succeeds, stopping when there are no more substitutions.

    Two of the less well known operations in sed are the ‘:label‘ and the ‘t‘ commands. They were present in the 7th Edition of Unix (circa 1978), though, so they are not new features. The first simply identifies a position in the script which can be jumped to with ‘b‘ (not wanted here) or ‘t‘:

    [2addr]t [label]
    

    Branch to the ‘:‘ function bearing the label if any substitutions have been made since the most recent reading of an input line or execution of a ‘t‘ function. If no label is specified, branch to the end of the script.

    Marvellous: we need:

     sed -e ':redo; s/\(\[[^] ]*\) /\1_/g; t redo' data.file
    

    Except – it doesn’t work all on one line like that (at least, not on MacOS X). This did work admirably, though:

    sed -e ':redo
            s/\(\[[^] ]*\) /\1_/g
            t redo' data.file
    

    Or, as noted in the comments, you could write three separate ‘-e’ options (which works on MacOS X):

     sed -e ':redo' -e 's/\(\[[^] ]*\) /\1_/g' -e 't redo' data.file
    

    Given the data file:

    a line with [one blank] word inside square brackets.
    a line with [two blank] or [three blank] words inside square brackets.
    a line with [no-blank] word inside square brackets.
    a line with [multiple words in a single bracket] inside square brackets.
    a line with [multiple words in a single bracket] [several times on one line]
    

    the output from the sed script shown is:

    a line with [one_blank] word inside square brackets.
    a line with [two_blank] or [three_blank] words inside square brackets.
    a line with [no-blank] word inside square brackets.
    a line with [multiple_words_in_a_single_bracket] inside square brackets.
    a line with [multiple_words_in_a_single_bracket] [several_times_on_one_line]
    

    And, finally, reading the fine print in the question, if you need this done only in the first square-bracketed field on each line, then we need to ensure that are no open square brackets before the one that starts the match. This variant works:

    sed -e ':redo' -e 's/^\([^]]*\[[^] ]*\) /\1_/' -e 't redo' data.file
    

    (The ‘g’ qualifier is gone – it probably isn’t needed in the other variants either given the loop; its presence might make the process marginally more efficient, but it would most likely be essentially impossible to detect that. The pattern is now anchored to the start of the line (the caret) and contains zero or more characters that are not open square bracket before the first open square bracket.)

    Sample output:

    a line with [two_blank] or [three blank] words inside square brackets.
    a line with [no-blank] word inside square brackets.
    a line with [multiple_words_in_a_single_bracket] inside square brackets.
    a line with [multiple_words_in_a_single_bracket] [several times on one line]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I figured out an algorithm that lets me turn my holed polygons into trapezoids
I think I finally figured out they need to use this DeclarativeFieldsMetaclass (to turn
After some trial and error (ok, just error) I figured out that c089b69c3d contained
I figured out that a constant literal get's placed in the data segment of
I figured out that CUDA does not work in 64bit mode on my mac
I figured out that of course . and SPACE aren't allowed. Are there other
I've figured out that the problem is only with the Build Settings tab. So
I've figured out that default ReceiveBufferSize (8192) doesn't work for me - I lose
I created an UIViewController subclass, and figured out that the default implementation of -loadView
I'm writing HFT trading software. I'm trying to optimize it. I figured out that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.