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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:55:49+00:00 2026-05-13T12:55:49+00:00

In a sh shell script. Given data in a text file: string1 string2 gibberish

  • 0

In a sh shell script.

Given data in a text file:

string1  
string2 gibberish  
gibberish  
string3 gibberish  
string4  

How could you use awk or sed to remove all lines between string2 (inclusive) and string3 (not including string3)?

to end up with:

string1  
string3  
string4  
  • 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-13T12:55:50+00:00Added an answer on May 13, 2026 at 12:55 pm

    you can try this. Anything before “string2” will not be deleted.

    awk 'BEGIN{f=0}
    {
        match($0,"string2")
        if(RSTART){
            print substr($0,1,RSTART-1)
            f=1
            next
        }
        match($0,"string3")
        if(RSTART){
            $0=substr($0,RSTART)
            f=0
        }
    }
    f==0{print}
    ' file
    

    output

    $ cat file
    string1 blah blah
    text before string2 junk
    gibberish
    gibberis string3 text here
    string4
    
    $ ./shell.sh
    string1 blah blah
    text before
    string3 text here
    string4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a text file containing records of the following form: ..... feGroup1Person1 Person ::=
I have a shell script that is created inside a php script (given full
Given this Unix shell script: test.sh: #!/bin/sh sleep 2 & sleep 5 & sleep
Given a shell script (with conditions, concatenations and interpolations) that builds and executes certain
Hello i want a simple shell script that find the name of the file
To write a shell script program to grep IP (from the list given) and
i am fixing a given shell script and i am getting following error message
I would to give the user the feature to run the shell script in
Possible Duplicate: What does “$?” give us exactly in a shell script? What does
In shell script i need to redirect output from dd command to /dev/null -

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.