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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:24:14+00:00 2026-05-12T06:24:14+00:00

Consider the input: =sec1= some-line some-other-line foo bar=baz =sec2= c=baz If I wish to

  • 0

Consider the input:

=sec1=
some-line
some-other-line

foo
bar=baz

=sec2=
c=baz

If I wish to process only =sec1= I can for example comment out the section by:

sed -e '/=sec1=/,/=[a-z]*=/s:^:#:' < input

… well, almost.

This will comment the lines including “=sec1=” and “=sec2=” lines, and the result will be something like:

#=sec1=
#some-line
#some-other-line
#
#foo
#bar=baz
#
#=sec2=
c=baz

My question is: What is the easiest way to exclude the start and end lines from a /START/,/END/ range in sed?

I know that for many cases refinement of the “s:::” claws can give solution in this specific case, but I am after the generic solution here.

In “Sed – An Introduction and Tutorial” Bruce Barnett writes: “I will show you later how to restrict a command up to, but not including the line containing the specified pattern.”, but I was not able to find where he actually show this.

In the “USEFUL ONE-LINE SCRIPTS FOR SED” Compiled by Eric Pement, I could find only the inclusive example:

# print section of file between two regular expressions (inclusive)
sed -n '/Iowa/,/Montana/p'             # case sensitive
  • 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-12T06:24:15+00:00Added an answer on May 12, 2026 at 6:24 am

    This should do the trick:

    sed -e '/=sec1=/,/=sec2=/ { /=sec1=/b; /=sec2=/b; s/^/#/ }' < input
    

    This matches between sec1 and sec2 inclusively and then just skips the first and last line with the b command. This leaves the desired lines between sec1 and sec2 (exclusive), and the s command adds the comment sign.

    Unfortunately, you do need to repeat the regexps for matching the delimiters. As far as I know there’s no better way to do this. At least you can keep the regexps clean, even though they’re used twice.

    This is adapted from the SED FAQ: How do I address all the lines between RE1 and RE2, excluding the lines themselves?

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

Sidebar

Related Questions

Background Consider the following input: <Foo Bar=bar Baz=1 Bax=bax > After processing, I need
Consider some function foo : def foo(input) input * 2 end How to get
Consider the following mark-up input: * Line 1 * Line 2 :* Line 2.1
Consider that I've a excel sheet in below format: person age Foo 29 Bar
Consider the following input as an example: [Ami]Song lyrics herp derp [F]song lyrics continue
Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
Consider the following three lines of Mathematica code and note that input line 1
Consider the following example: ( demo here ) HTML: <input type=text /> <div>Click here
Consider a hiddenfield in the page HfId <input type=hidden id=HfId/> How to assign,clear and
Consider the following HTML snipped: <input type=submit value=Delete selected><input type=submit value=Print selected> <div class=table>

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.