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 6849621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:00:52+00:00 2026-05-27T01:00:52+00:00

I have this test file. [root@localhost ~]# cat f.txt a aa MM bbb b

  • 0

I have this test file.

[root@localhost ~]# cat f.txt 
"a aa"  MM  "bbb  b"
MM    MM
MM"b b "
[root@localhost ~]#

I want to replace all space characters in the quotes, note, just in the quotes. All characters out of the quotes should not be touched. That is to say, what I want is something similar to:

"a_aa"  MM  "bbb__b"
MM    MM
MM"b_b_"

Can this be implemented using sed?

Thanks,

  • 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-27T01:00:53+00:00Added an answer on May 27, 2026 at 1:00 am

    This is an entirely non-trivial question.

    This works replacing the first space inside quotes with underscore:

    $ sed 's/\("[^ "]*\) \([^"]*"\)/\1_\2/g' f.txt
    "a_aa"  MM  "bbb_ b"
    MM    MM
    MM"b_b "
    $
    

    For this example, where there are no more than two spaces inside any of the quotes, it is tempting to simply repeat the command, but it gives an incorrect result:

    $ sed -e 's/\("[^ "]*\) \([^"]*"\)/\1_\2/g' \
    >     -e 's/\("[^ "]*\) \([^"]*"\)/\1_\2/g' f.txt
    "a_aa"_ MM  "bbb_ b"
    MM    MM
    MM"b_b_"
    $
    

    If your version of sed supports ‘extended regular expressions’, then this works for the sample data:

    $ sed -E \
    >    -e 's/^(([^"]*("[^ "]*")?)*)("[^ "]*) ([^"]*")/\1\4_\5/' \
    >    -e 's/^(([^"]*("[^ "]*")?)*)("[^ "]*) ([^"]*")/\1\4_\5/' \
    >    -e 's/^(([^"]*("[^ "]*")?)*)("[^ "]*) ([^"]*")/\1\4_\5/' \
    >    f.txt
    "a_aa"  MM  "bbb__b"
    MM    MM
    MM"b_b_"
    $
    

    You have to repeat that ghastly regex for every space within double quotes – hence three times for the first line of data.

    The regex can be explained as:

    • Starting at the beginning of a line,
    • Look for sequences of ‘zero or more non-quotes, optionally followed by a quote, no spaces or quotes, and a quote’, the whole assembly repeated zero or more times,
    • Followed by a quote, zero or more non-quotes, non-spaces, a space, and zero or more non-quotes, and a quote.
    • Replace the matched material with the leading part, the material at the start of the current quoted passage, an underscore, and the trailing material of the current quoted passage.

    Because of the start anchor, this has to be repeated once per blank…but sed has a looping construct, so we can do it with:

    $ sed -E -e ':redo
    >            s/^(([^"]*("[^ "]*")?)*)("[^ "]*) ([^"]*")/\1\4_\5/
    >            t redo' f.txt
    "a_aa"  MM  "bbb__b"
    MM    MM
    MM"b_b_"
    $
    

    The :redo defines a label; the s/// command is as before; the t redo command jumps to the label if there was any substitution done since the last read of a line or jump to a label.


    Given the discussion in the comments, there are a couple of points worth mentioning:

    1. The -E option applies to sed on MacOS X (tested 10.7.2). The corresponding option for the GNU version of sed is -r (or --regex-extended). The -E option is consistent with grep -E (which also uses extended regular expressions). The ‘classic Unix systems’ do not support EREs with sed (Solaris 10, AIX 6, HP-UX 11).

    2. You can replace the ? I used (which is the only character that forces the use of an ERE instead of a BRE) with *, and then deal with the parentheses (which require backslashes in front of them in a BRE to make them into capturing parentheses), leaving the script:

      sed -e ':redo
              s/^\(\([^"]*\("[^ "]*"\)*\)*\)\("[^ "]*\) \([^"]*"\)/\1\4_\5/g
              t redo' f.txt
      

      This produces the same output on the same input – I tried some slightly more complex patterns in the input:

      "a aa"  MM  "bbb  b"
      MM    MM
      MM"b b "
      "c c""d d""e  e" X " f "" g "
       "C C" "D D" "E  E" x " F " " G "
      

      This gives the output:

      "a_aa"  MM  "bbb__b"
      MM    MM
      MM"b_b_"
      "c_c""d_d""e__e" X "_f_""_g_"
       "C_C" "D_D" "E__E" x "_F_" "_G_"
      
    3. Even with BRE notation, sed supported the \{0,1\} notation to specify 0 or 1 occurrences of the previous RE term, so the ? version could be translated to a BRE using:

      sed -e ':redo
              s/^\(\([^"]*\("[^ "]*"\)\{0,1\}\)*\)\("[^ "]*\) \([^"]*"\)/\1\4_\5/g
              t redo' f.txt
      

      This produces the same output as the other alternatives.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is driving me crazy. I have this one php file on a test
If I have a Resource bundle property file: A.properties: thekey={0} This is a test
I have DocumentRoot /var/www/test in my .htaccess file. This is causing the apache server
I have this code in my controller and want to test this code line
I have this test case def setUp(self): self.user = User.objects.create(username=tauri, password='gaul') def test_loginin_student_control_panel(self): c
Note: I'm using Google Chrome Currently I have this test page http://www.evecakes.com/doodles/canvas_size_issue.htm It should
I have this JUnit test that I need help developing a Interface and Class
I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
This is an erlang problem, it seems. I have this code to test the
I have this Javascript data: [{id:123,type:test},{id:154,type:another}] How would you transform that into something so

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.