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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:11:43+00:00 2026-06-08T13:11:43+00:00

My problem is similar problem to shell script: search and replace over multiple lines

  • 0

My problem is similar problem to shell script: search and replace over multiple lines with a small exception.

In the question linked the user wants to do this:

source:
[stuff before]
<!--WIERD_SPECIAL_COMMENT_BEGIN-->
  [stuff here, possibly multiple lines.
<!--WIERD_SPECIAL_COMMENT_END-->
[stuff after]    

target:
[stuff before]
[new content]
[stuff after]

My problem is similar, I want to do this:

source:
[stuff before]
<!--WIERD_SPECIAL_COMMENT_BEGIN-->
  [this]
<!--WIERD_SPECIAL_COMMENT_END-->
<!--WIERD_SPECIAL_COMMENT_BEGIN-->
  [not this]
<!--WIERD_SPECIAL_COMMENT_END-->
[stuff after]    

target:
[stuff before]
[new content]
<!--WIERD_SPECIAL_COMMENT_BEGIN-->
  [not this]
<!--WIERD_SPECIAL_COMMENT_END-->
[stuff after]

In a proper multiline regex this is easy to do:

/<!--WIERD_SPECIAL_COMMENT_BEGIN-->.*[this].*<!--WIERD_SPECIAL_COMMENT_END-->/m

but the answer suggested in the linked question uses regex as ranges which doesn’t allow checking lines between the two outlying bounds.

Is there any way to add all the lines in a range to the pattern buffer so I can regex over all the lines at once? eg:

sed '
    #range between comment beginning and comment end
    /<!--WIERD_SPECIAL_COMMENT_BEGIN-->/,/<!--WIERD_SPECIAL_COMMENT_END-->/
    #Do something to add the lines in this range to pattern buffer
    /.*[this].*/d
    #Delete all the lines if [this] is in the pattern buffer
' <in.txt >out.txt
  • 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-08T13:11:47+00:00Added an answer on June 8, 2026 at 1:11 pm

    With Perl, it’s relatively simple.

    perl -0777pe 's/<!--BEGIN-->\n(?:(?!<!--END-->\n).)*?\[this\].*?\n<!--END-->\n/[new content]\n/s' in.txt
    

    The benefits offered by Perl are (a) the -0777 “slurp mode” which pulls in the entire input file in one go, instead of sed‘s line-at-a-time processing; (b) the /s regex flag which allows for dot to match a newline; (c) the stingy repetition operators *? and friends, which causes the repetition to match as little as possible instead of as much as possible; and finally (d) the negative lookahead (?!...) which allows you to inhibit matching where the negative lookahead expression matches. (Without this, even stingy matching would match across an end delimiter if there was a “false” starting delimiter in the “stuff before” text.) … And of course, (e) a general-purpose programming language where sed is only suitable for relatively simple text processing tasks.

    (I used simpler beginning and ending delimiters. I hope “wierd” was an intentional misspelling.)

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

Sidebar

Related Questions

I want to write a shell script which will find the occurence of multiple
Related to a similar problem I'm having: zsh not re-computing my shell prompt Is
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a problem similar to the egg and the chicken. I have two
My problem seems similar to Not able to visualize a loaded data , but
My problem is similar to Django Passing Custom Form Parameters to Formset Ive have
I have similar problem to one discussed here , but with stronger practical usage.
This problem is similar to when creating alternate colour table-rows, only we are dealing
I suppose similar problem would have been discussed here, but I couldn't find it.
Earlier today I asked similar problem to find the maximum element which is common

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.