read numbers from file and output which numbers belongs to which range

Howdy experts,

We have some ranges of number which belongs to particual group as below.

GroupNo	    StartRange	   EndRange

Group0125	935300	             935399
Group2006	935400	             935476
	        937430	             937459
Group0324	935477	             935549
                937480	             937499
Group0404	935550	             935625
                937800	             937879

Now I have a list of numbers in a file. Script will read the list of numbers in the file and output which numbers belongs to which group.

Example File1:
935303455
935479875
935477987
937499000
937492355
937860439
935393444

I need your help to write this script.

Best regards,
purple

And what should be the expected output given your sample data?
Considering your description, it should be something like this
(use gawk, nawk or /usr/xpg4/bin/awk on Solaris)

awk 'NR == FNR && NR > 2 {
  if (NF < 3) v = $1 SUBSEP $2
  else { g = $1; v = $2 SUBSEP $3 }
  r[g] = r[g] ? r[g] SUBSEP v : v; next 
  }
{ for (g in r) {
    n = split(r[g], t, SUBSEP)
	for (i=1; i<=n; i+=2)
	  if (t <= $1 && $1 <= t[i+1]) {
	    print $1, "is in", g; break
		}
	    }
	}
	' range_file data_file

But given your sample data, it seems more like this:

awk 'NR == FNR && NR > 2 {
  if (NF < 3) v = $1 SUBSEP $2
  else { g = $1; v = $2 SUBSEP $3 }
  r[g] = r[g] ? r[g] SUBSEP v : v; next 
  }
{ v = substr($1, 1, 6)
  for (g in r) {
    n = split(r[g], t, SUBSEP)
	for (i=1; i<=n; i+=2)
	  if (t <= v && v <= t[i+1]) {
	    print v, "is in", g; break
		}
	    }
	}
	' range_file data_file

Output will be simple.

935303455 groupxxxx
935479875 groupxxxx
935477987 groupxxxx
937499000 groupxxxx

In your sample there are no matching groups for the numbers in your example data, that's what I mean ...

There is matchings see below- At least below is matched. In this way if no matching than output will be No matched. If match then output will be accordingly.

935303455 number belongs to below group
start end
Group0125 935300 935399

Please help.

Try the below code :

 
 
for i in `cat inp.txt`
do
j=`echo $i |awk '{ print substr($0,1,6) }'`
echo $j
awk -v inp=$j -v inpr=$i  'NF==3{tit=$1};NF==3{if(inp > $2 && inp < $3) print $1"----->"inpr};NF==2{if(inp > $1 && inp < $2) print tit"----->"inpr}' range_ref.txt >> output.txt
done

here wht i hav done :

 
TESTING>cat inp.txt
935303455
935479875
935477987
937499000
937492355
937860439
935393444
937431000
 
TESTING>cat range_ref.txt
Group0125       935300               935399
Group2006       935400               935476
                     937430               937459
Group0324       935477               935549
                     937480               937499
Group0404       935550               935625
                     937800               937879
 
TESTING>./awk_scr.sh
TESTING>cat output.txt
 
Group0125----->935303455
Group0324----->935479875
Group0324----->937492355
Group0404----->937860439
Group0125----->935393444
Group2006----->937431000

if you have Python

groups={
 "Group0125":map(str,range(935300,935400)),
 "Group2006":map(str,range(935400,935477)+range(935477,935550)),
 "Group0324":map(str,range(935300,935400)+range(937480,937500)),
 "Group0404":map(str,range(935550,935626)+range(937800, 937880))
}
for line in open("file"):
    line=line.strip()
    for key,val  in groups.iteritems():
        if line[:6] in val:
            print key + " --------> " + line[:6]
            break

output

# ./test.py
Group0324 --------> 935303
Group2006 --------> 935479
Group2006 --------> 935477
Group0324 --------> 937499
Group0324 --------> 937492
Group0404 --------> 937860
Group0324 --------> 935393