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

  • Home
  • SEARCH
  • 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 8103597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:35:21+00:00 2026-06-05T23:35:21+00:00

Let’s say we want to do some substitutions only between some patterns, let them

  • 0

Let’s say we want to do some substitutions only between some patterns, let them be <a> and </a> for clarity… (all right, all right, they’re start and end!.. Jeez!)

So I know what to do if start and end always occur on the same line: just design a proper regex.

I also know what to do if they’re guaranteed to be on different lines and I don’t care about anything in the line containing end and I’m also OK with applying all the commands in the line containing start before start: just specify the address range as /start/,/end/.

This, however, doesn’t sound very useful. What if I need to do a smarter job, for instance, introduce changes inside a {...} block?

One thing I can think of is breaking the input on { and } before processing and putting it back together afterwards:

sed 's/{\|}/\n/g' input | sed 'main stuff' | sed ':a $!{N;ba}; s/\n\(}\|{\)\n/\1/g'

Another option is the opposite:

cat input | tr '\n' '#' | sed 'whatever; s/#/\n/g'

Both of these are ugly, mainly because the operations are not confined within a single command. The second one is even worse because one has to use some character or substring as a “newline holder” assuming it isn’t present in the original text.

So the question is: are there better ways or can the above-mentioned ones be optimized? This is quite a regular task from what I read in recent SO questions, so I’d like to choose the best practice once and for all.

P.S. I’m mostly interested in pure sed solutions: can the job be do with one invocation of sed and nothing else? Please no awk, Perl, etc.: this is more of a theoretical question, not a “need the job done asap” one.

  • 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-06-05T23:35:23+00:00Added an answer on June 5, 2026 at 11:35 pm

    This might work for you:

    # create multiline test data
    cat <<\! >/tmp/a
    > this
    > this { this needs
    > changing to
    > that } that
    > that
    > !
    sed '/{/!b;:a;/}/!{$q;N;ba};h;s/[^{]*{//;s/}.*//;s/this\|that/\U&/g;x;G;s/{[^}]*}\([^\n]*\)\n\(.*\)/{\2}\1/' /tmp/a
    this
    this { THIS needs
    changing to
    THAT } that
    that
    # convert multiline test data to a single line
    tr '\n' ' ' </tmp/a >/tmp/b
    sed '/{/!b;:a;/}/!{$q;N;ba};h;s/[^{]*{//;s/}.*//;s/this\|that/\U&/g;x;G;s/{[^}]*}\([^\n]*\)\n\(.*\)/{\2}\1/' /tmp/b
    this this { THIS needs changing to THAT } that that
    

    Explanation:

    • Read the data into the pattern space (PS). /{/!b;:a;/}/!{$q;N;ba}
    • Copy the data into the hold space (HS). h
    • Strip non-data from front and back of string. s/[^{]*{//;s/}.*//
    • Convert data e.g. s/this\|that/\U&/g
    • Swap to HS and append converted data. x;G
    • Replace old data with converted data.s/{[^}]*}\([^\n]*\)\n\(.*\)/{\2}\1/

    EDIT:

    A more complicated answer which I think caters for more than one block per line.

    # slurp file into pattern space (PS)
    :a
    $! {
    N
    ba
    }
    # check for presence of \v if so quit with exit value 1
    /\v/q1
    # replace original newlines with \v's
    y/\n/\v/
    # append a newline to PS as a delimiter
    G
    # copy PS to hold space (HS)
    h
    # starting from right to left delete everything but blocks
    :b
    s/\(.*\)\({.*}\).*\n/\1\n\2/
    tb
    # delete any non-block details form the start of the file
    s/.*\n//
    # PS contains only block details
    # do any block processing here e.g. uppercase this and that
    s/th\(is\|at\)/\U&/g
    # append ps to hs
    H
    # swap to HS
    x
    # replace each original block with its processed one from right to left
    :c
    s/\(.*\){.*}\(.*\)\n\n\(.*\)\({.*}\)/\1\n\n\4\2\3/
    tc
    # delete newlines
    s/\n//g
    # restore original newlines
    y/\v/\n/
    # done!
    

    N.B. This uses GNU specific options but could be tweaked to work with generic sed’s.

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

Sidebar

Related Questions

Let's say I want to have some kind of a cache that did something
Let's say there is a graph and some set of functions like: create-node ::
Let say I've this URL: http://example.com/image-title/987654/ I want to insert download to the part
Let's say I got a Map<String, String> and I wanna remove all the entries
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say I have a drive such as C:\ , and I want to
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have thousands of users and I want to make the passwords
Let's say I have an (x,y) that is always the same for the start

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.