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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:02:26+00:00 2026-06-16T05:02:26+00:00

I have a question on pattern matching. I have a file with multiple patterns

  • 0

I have a question on pattern matching.
I have a file with multiple patterns in it, say pattern.txt

Locus3039v1rpkm6.85
Locus3041v1rpkm6.84
Locus3042v1rpkm6.84

And the test file to search is file.txt –

Locus3039v1rpkm6.85 gi|350401309|ref|XM_003486067.1|    0   10  85  328 253 8e-12   78.8
Locus3039v1rpkm6.85 gi|350401301|ref|XM_003486066.1|    0   10  85  566 491 8e-12   78.8
Locus3039v1rpkm6.85 gi|350401298|ref|XM_003486065.1|    0   10  85  500 425 8e-12   78.8
Locus3039v1rpkm6.85 gi|340723355|ref|XM_003400008.1|    0   10  106 566 470 3e-11   77.0
Locus3039v1rpkm6.85 gi|340723353|ref|XM_003400007.1|    0   10  106 496 400 3e-11   77.0
Locus3039v1rpkm6.85 gi|359323056|ref|XM_003639939.1|    0   27  104 322 245 9e-05   55.4
Locus3039v1rpkm6.85 gi|359323055|ref|XM_543849.4|   0   27  104 241 164 9e-05   55.4
Locus3039v1rpkm6.85 gi|354503991|ref|XM_003514015.1|    0   27  103 335 259 0.004   50.0
Locus3039v1rpkm6.85 gi|341599927|emb|AM412059.2|    1   63  100 1645525 1645489 6.8 39.2
Locus3039v1rpkm6.85 gi|340003223|emb|HE572590.1|    1   63  100 1671652 1671616 6.8 39.2
Locus3041v1rpkm6.84 gi|337757426|emb|FQ859181.1|    1   61  114 2772617 2772667 0.60    42.8
Locus3041v1rpkm6.84 gi|159889572|gb|CP000875.1|     0   5   40  1185295 1185330 0.60    42.8
Locus3041v1rpkm6.84 gi|158107272|gb|CP000820.1|     0   2   34  5594193 5594161 0.60    42.8
Locus3041v1rpkm6.84 gi|156844486|ref|XM_001645256.1|    83  140 793 850 0.60    42.8
Locus3041v1rpkm6.84 gi|339305108|gb|CP001503.2|     0   58  94  3006529 3006565 2.1 41.0
Locus3041v1rpkm6.84 gi|247533203|gb|CP001607.1|     0   1   40  1268073 1268034 2.1 41.0
Locus3041v1rpkm6.84 gi|367050653|ref|XM_003655658.1|    0   75  103 843 871 7.3 39.2
Locus3041v1rpkm6.84 gi|347002178|gb|CP003012.1|     0   75  103 2986236 2986208 7.3 39.2
Locus3043v1rpkm6.84 gi|332015867|gb|HQ658110.1|     0   9   31  4151    4129    0.49    42.8
Locus3043v1rpkm6.84 gi|254946573|gb|CP001619.1|     1   9   43  4243052 4243019 0.49    42.8
Locus3043v1rpkm6.84 gi|329755665|gb|JF715057.1|     0   11  42  110968  110937  1.7 41.0
Locus3043v1rpkm6.84 gi|9937515|gb|AF294752.1|   0   48  79  2081    2050    1.7 41.0

I want to match each pattern for the first 5 hits and move to the next pattern for the 1st five and so on.

I tried

 grep -i -m 5 -f pattern.txt file.txt > out.txt
 grep -i -f pattern.txt -m 5 file.txt > out.txt

But I am getting only the top 5 for the first pattern and ending. Where am I going wrong? Is there a parameter to perform this required function?

  • 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-16T05:02:27+00:00Added an answer on June 16, 2026 at 5:02 am

    Try this:

    for pat in $(cat pattern.txt); do grep -i -m 5 $pat file.txt; done > out.txt
    

    Which means

    1. For each pattern in pattern.txt, grep the first 5 matched record.
    2. Append the result to out.txt

    EDIT

    As @dogbane mentioned in his comment, this is a UUOC. Here is my improved answer:

    for pat in $(< pattern.txt); do grep -i -m 5 $pat file.txt; done > out.txt
    

    Also look at this answer.

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

Sidebar

Related Questions

I have a question on pattern matching: Is it possible to somehow match a
Actually, this question seems to have two parts: How to implement pattern matching? How
I'm fairly new to Haskell and have a question about pattern-matching. Here is a
I have a question about the singleton pattern. I saw two cases concerning the
I'm using CakePHP but it's a question about the MVC pattern. I have in
I have been reading a lot on MVC/MVP patterns.... I have a simple question....If
I have a text file Qfile.txt and the contents are follows, and want to
I have strings of 15 characters long. I am performing some pattern matching on
Let's say I have an input text file of the following format: Section1 Heading
I'm teaching myself Scala and I have sort of a philosophical question. Is pattern

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.