Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK or something else?

Status
Not open for further replies.

TS2001

Technical User
Dec 10, 2013
1
0
0
US
Hi,

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

File1:
Low High
14000 20000
25000 41000
60000 80000

File2:
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!

thanks!
 
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" )
   arr[$2]=$1
}
{
for(ind in arr)
   if(arr[ind] > $1 && arr[ind] < $2)
      print arr[ind]" "ind
} ' file1
 
You may try this:
Code:
awk 'NR>1{system("awk '"'"'$1>="$1"&&$1<="$2"'"'"' File2")}' File1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top