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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:34:50+00:00 2026-06-13T05:34:50+00:00

I am new to scripting and was trying to learn how to extract any

  • 0

I am new to scripting and was trying to learn how to extract any text that exists between two different patterns. However, I am still not able to figure out how to extract text between two patterns in the following scenario:

If I have my input file reading:

Hi I would like
to print text
between these 
patterns

and my expected output is like:

I would like
to print text
between these 

i.e. my first search pattern is “Hi’ and skip this pattern, but print everything that exists in the same line following that matched pattern. My second search pattern is “patterns” and I would like to completely avoid printing this line or any lines beyond that.

I tried the following:

sed -n '/Hi/,/patterns/p' test.txt 

[output]

Hi I would like
to print text
between these 
patterns 

Next, I tried:

`awk ' /'"Hi"'/ {flag=1;next} /'"pattern"'/{flag=0} flag { print }'` test.txt 

[output]

to print text
between these

Can someone help me out in identifying how to achieve this?
Thanks in advance

  • 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-13T05:34:51+00:00Added an answer on June 13, 2026 at 5:34 am

    You have the right idea, a mini-state-machine in awk but you need some slight mods as per the following transcript:

    pax> echo 'Hi I would like
    to print text
    between these 
    patterns ' | awk '
        /patterns/ { echo = 0 }
        /Hi /      { gsub("^.*Hi ", "", $0); echo = 1 }
                   { if (echo == 1) { print } }'
    

    Or, in compressed form:

    awk '/patterns/{e=0}/Hi /{gsub("^.*Hi ","",$0);e=1}{if(e==1){print}}'
    

    The output of that is:

    I would like
    to print text
    between these 
    

    as requested.

    The way this works is as follows. The echo variable is initially 0 meaning that no echoing will take place.

    Each line is checked in turn. If it contains patterns, echoing is disabled.

    If it contains Hi followed by a space, echoing is turned on and gsub is used to modify the line to get rid of everything up to the Hi.

    Then, regardless, the line (possibly modified) is echoed when the echo flag is on.

    Now, there’s going to be edge cases such as:

    • lines containing two occurrences of Hi; or
    • lines containing something before the patterns.

    You haven’t specified how they should be handled so I didn’t bother, but the basic concept should be the same.

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

Sidebar

Related Questions

I am new to bash scripting and trying to learn a few things. Here
I'm really new to UNIX/Shell Scripting I'm trying to extract disk usage from numerous
I am new at shell scripting and I am trying to read data from
I am trying to build a script (I'm pretty new to linux scripting) and
I am new to shell scripting and am trying to get all the android
New to bash scripting, The previous answers didn't helped me. I am trying to
I am new to shell scripting and i am trying to remove new line
I'm very new to shell scripting and I am trying to get to grips
I am new to bash-scripting & trying to understand how things work. It's all
I am very new to Bash scripting. I am trying to write a script

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.