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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:59:37+00:00 2026-06-10T00:59:37+00:00

I have a file dict containing one integer on each row 123 456 I

  • 0

I have a file dict containing one integer on each row

123
456

I want to find lines in file file that contain exactly the integers in dict.

If I use

$ grep -w -f dict file

I get false matches such as

12345  foo
23456  bar

These are false because 12345 != 123 and 23456 != 456. The problem is that the -w option considers digits as word characters too. The -x option will not work either as the lines in file can have other text. What’s the best way to do this please? It will be great if the solution can offer progress monitoring and a good performance on dict and file of large sizes.

  • 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-10T00:59:39+00:00Added an answer on June 10, 2026 at 12:59 am

    A fairly general method using awk:

    awk 'FNR==NR { array[$1]++; next } { for (i=1; i<=NF; i++) if ($i in array) print $0 }' dict file
    

    Explanation:

    FNR==NR { }  ## FNR is number of records relative to the current input file. 
                 ## NR is the total number of records.
                 ## So this statement simply means `while we're reading the 1st file
                 ## called dict; do ...`
    
    array[$1]++; ## Add the first column ($1) to an array called `array`.
                 ## I could use $0 (the whole line) here, but since you have said
                 ## that there will only be one integer per line, I decided to use
                 ## $1 (it strips leading and lagging whitespace; if any)
    
    next         ## process the next line in `dict`
    
    for (i=1; i<=NF; i++)  ## loop through each column in `file`
    
    if ($i in array)       ## if one of these columns can be found in the array
    
    print $0               ## print the whole line out
    

    To process multiple files using bash loop:

    ## This will process files; like file, file1, file2, file3 ...
    ## And create output files like, file.out, file1.out, file2.out, file3.out ...
    
    for j in file*; do awk -v FILE=$j.out 'FNR==NR { array[$1]++; next } { for (i=1; i<=NF; i++) if ($i in array) print $0 > FILE }' dict $j; done
    

    If you are interested in using tee on multiple files, you may like to try something like this:

    for j in file*; do awk -v FILE=$j.out 'FNR==NR { array[$1]++; next } { for (i=1; i<=NF; i++) if ($i in array) { print $0 > FILE; print FILENAME, $0 } }' dict $j; done 2>&1 | tee output
    

    This will show you the name of the file being process and the matching record found, and write a ‘log’ to file called output.

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

Sidebar

Related Questions

I have a dict that describes a mapping I want applied to every row
I have a file that I'm working with. I want to create a function
I have a .plist file containing a dictionary item, something like this: <dict> <dict>
I have a fairly large pickled (dict) file in binary format that takes few
I have to read a dict.txt file which contains one string for line and
I have a lib/languages.py file only with a dict format e.g.: en = {}
I have file with lines like so: 1 17 A G R:560:500:60:10.71%:1.6329E-19 Pass:1.0:276:0:57:0:1E0 15
I have file in json format of world-countries I want to change it to
Suppose that we have file like this: sometext11 sometext12 sometext13 sometext21 sometext22 sometext23 Texts
We have an Excel file with a worksheet containing people records. 1. Phone Number

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.