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

The Archive Base Latest Questions

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

I am attempting to return the first occurrence of multiple strings, ie, I want

  • 0

I am attempting to return the first occurrence of multiple strings, ie, I want to select the lines from the following text where the first occurrence of 1259, 3009, and 1589 happen.

ADWN    1259    11:00   B23

ADWN    3009    12:00   B19

DDWN     723    11:30   B04

ADWN    1589    14:20   B12

ADWN    1259    11:10   B23

DDWN    2534    13:00   B16

ADWN    3009    11:50   B14

This gives me all matches:

grep '1259\|3009\|1589'  somelog.log

And this gives me only the first match

grep -m 1  '1259\|3009\|1589'  somelog.log

I want to return the following:

ADWN    1259    11:00   B23

ADWN    3009    12:00   B19

ADWN    1589    14:20   B12

I think that creating a file with the required values, and then looping through the file, passing each number individually into the grep command will give me what I am looking for, but I haven’t found an example of this. Is there a simple solution for this, is a loop the best way to handle this, or has this example already been answered elsewhere?

Thanks in advance for your ideas and suggestions–

Clyde

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

    One way using awk:

    awk '!array[$2]++ && $2 ~ /^1259$|^3009$|^1589$/' file.txt
    

    Results:

    ADWN    1259    11:00   B23
    ADWN    3009    12:00   B19
    ADWN    1589    14:20   B12
    

    edit:

    I should really get into the habit of reading the whole question first. I see that you’re thinking of creating a file with the values you’d like to find the first occurrence of. Put these in a file called values.txt with one value per line. For example; here’s the contents of values.txt:

    1259
    3009
    1589
    

    Then run this:

    awk 'FNR==NR { array[$0]++; next } $2 in array { print; delete array[$2] }' values.txt file.txt
    

    Results:

    ADWN    1259    11:00   B23
    ADWN    3009    12:00   B19
    ADWN    1589    14:20   B12
    

    1st command explanation:

    If the second column ($2) equals one of those three values listed, add it to the array if it’s not already in there. awk prints the whole line by default.

    2nd command explanation:

    FNR is number of records relative to the current input file.
    NR is the total number of records.

    The FNR==NR { ... } construct is only true for the first input file. So for each of the lines in values.txt, we add the whole line ($0) to an array (I’ve called it array, but you could give it another name). next forces awk to read the next line in values.txt (and skip processing the rest of the command). When FNR==NR is no longer true, the second file in the arguments list is read. We then check for the second column ($2)in the array, if it’s in there, print it and remove it from the array. By using delete we essentially set a max count of one.

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

Sidebar

Related Questions

I am attempting to return a rowcount from a subquery as part of a
I'm attempting to write my first custom Cocoa Framework and I want to expose
I currently have the following SQL statement: SELECT s.first_name, s.last_name, tgs.group_identifier FROM staff s
Here's the problem code I'm attempting to compile: bool TeamMatcher::simpleComparator(Student first, Student second){ return
I'm attempting to data bind JSON data return from a Grails controller to a
I'm attempting to write some jquery from the first time and I'm using the
I'm attempting to use Dapper to return a set of Shares and an associated
I'm attempting to write a script that returns rows from a mySQL database. Basically,
Attempting to set up an automated texting service for customers, where people can text
Attempting to use the data series from this example no longer passes the JSONLint

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.