Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

AWK or something else?

AWK or something else?

AWK or something else?


I am new to awk, so please bear with me! I have two files:

Low High
14000 20000
25000 41000
60000 80000

Income Name
12000 smith
15000 adam
22000 john
26000 denver

I want to loop through File1, and for each range (Low to High) extract the rows in File2. So my output file should look like

Desired output:
15000 adam
26000 denver

Using awk I can loop through File2, extract the Income for each row, but how do I see if the Income is within the range in File1? My code for looping:

awk '{ for (i = 1; i <= NR; i++) print $1 }' File2

Any help guidance would be appreciated!


RE: AWK or something else?

In file2, assuming field2 is unique, read file2 into an array. Then, read file1 checking if each array element is between file1 and field2:

CODE -->

awk ' BEGIN {
while( getline < "file2" )
for(ind in arr)
   if(arr[ind] > $1 && arr[ind] < $2)
      print arr[ind]" "ind
} ' file1 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close