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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:25:45+00:00 2026-05-17T15:25:45+00:00

So my dear SOers, Let me be direct to the point: specification: filter a

  • 0

So my dear SOers, Let me be direct to the point:
specification: filter a text file using pairs of patterns.
Example: if we have a file:

line 1 blabla
line 2 more blabla
line 3 **PAT1a** blabla
line 4 blabla
line 5 **PAT1b** blabla
line 6 blabla
line 7 **PAT2a** blabla
line 8 blabla
line 9 **PAT2b** blabla
line 10 **PAT3a** blabla
line 11 blabla
line 12 **PAT3b** blabla
more and more blabla

should give:

line 3 **PAT1a** blabla
line 4 blabla
line 5 **PAT1b** blabla
line 7 **PAT2a** blabla
line 8 blabla
line 9 **PAT2b** blabla
line 10 **PAT3a** blabla
line 11 blabla
line 12 **PAT3b** blabla

I know how to filer only one part of it using ‘sed’:
sed -n -e ‘/PAT1a/,/PAT1b/{p}’
But how to filter all the snippets, do i need to write those pairs of patterns in a configuration file, read a pair from it, use the sed cmd above, go to next pair…?

Note: Suppose PAT1, PAT2 and PAT3, etc share no common prefix(like ‘PAT’ in this case)

One thing more: how to make a newline in quota text in this post without leaving a whole blank line?

  • 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-17T15:25:45+00:00Added an answer on May 17, 2026 at 3:25 pm

    I assumed the pattern pairs are given as a separate file. Then, when they appear in order in the input, you could use this awk script:

    awk 'NR == FNR { a[NR] = $1; b[NR] = $2; next }
         !s && $0 ~ a[i+1] { s = 1 }
         s
         s && $0 ~ b[i+1] { s = 0; i++ }' patterns.txt input.txt
    

    And a more complicated version when the patterns can appear out of order:

    awk 'NR == FNR { a[++n] = $1; b[n] = $2; next }
         { for (i = 1; !s && i <= n; i++) if ($0 ~ a[i]) s = i; }
         s
         s && $0 ~ b[s] { s = 0 }' patterns.txt input.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

dear all..i have a textfield <tr> <td> <td><input type=text id=model_name></td> </td> </tr> and a
Dear all,I am now using a webtool http://fiddesktop.cs.northwestern.edu/mmp/scrape?url= to parse a webpage. For example,we
Dear gods of Stackoverflow Let's say I have a MySQL query that selects a
Dear Masters of The Command Line, I have a directory tree for which I
Dear community members, I have a small problem, which involves a text input field
Dear Stacktoverflow, can you show me an example of how to use a QScrollBar?
Dear g++ hackers, I have the following question. When some data of an object
Dear all, I have a question about Facebook Page: ( NOT user profile page,
Dear all,Now i have this question in my java program,I think it should be
Dear All, please help me since I'm newbie in SQL Server. I have a

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.