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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:09:11+00:00 2026-05-31T12:09:11+00:00

I read through the forum for clues how to solve my problem, but none

  • 0

I read through the forum for clues how to solve my problem, but none of the related threads are useable for me, with limited programming knowledge, to apply to my specific problem.

My problem is this: I need to get rid of garbage lines that are clustered throughout my file, but are in between clusters of useable lines. I searched the sed manual and other informative sources about deleting ranges that match patterns, but they only mention to delete UNTIL match pattern, not TILL.

Now I would like to specify a range for which sed deletes lines starting from the first line that matches the pattern line till the line that matches the other pattern. Furthermore, sed needs to recognize the patterns that exists at the end of the lines.

For example:

line 1
blah blah 1
blah blah 2
blah blah 3
blah blah 4
line 2
line 3

Result needs to be:

line 1
blah blah 1
line 2
line 3

Please note the multiple lines between line and and line 2. While blah blah 1 needs to stay, the other 3 need to be deleted.

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-31T12:09:12+00:00Added an answer on May 31, 2026 at 12:09 pm

    Try this

    sed -n '/line 1/{;p;n;p;};/line 2/,$p'  sedTest1.txt
    
    #output
    line 1
    blah blah 1
    line 2
    line 3
    

    Sed deconstructed :

     sed -n '/line 1/{;p;n;p;};/line 2/,$p'  sedTest1.txt
         |    |        |        |      |||-> print the range
         |    |        |        |      ||-> til end of file (the '$' char)
         |    |        |        |      |-> range operator (i.e. start,end)
         |    |        |        |-> beginning of range to watch for and print
         |    |        |-> now print line, get 'n'ext, print that line 
         |    |-> match the line with text 'line 1'
         |-> don't print every line, only ones flagged with 'p'
    

    Read this from the bottom up.

    Also, as your data is a sample, AND you refer to it as garbage lines, it may not be this simple.
    You’ll need to look through sed tutorials to get up to speed.

    I hope this helps.

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

Sidebar

Related Questions

I read through quite a few similar questions here on SO but none were
I've read through a few other threads on here, though none of them really
I've read through many of the drag and drop threads on SO and i
I've read through the two other threads that extract the dll from the application
I've gone through all the tutorials and read through a ton of forum posts,
I'm still quite new to C#, but noticed the advantages through forum postings of
I need to read through the XML file that contains info but one at
I have looked through the answers in this forum but cannot seem to find
I'm pretty new to all this. I've read through the forums, but can't seem
I've read through a lot of questions addressing similar question but I can't get

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.