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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read numbers from file and output which numbers belongs to which range
# 1  
Old 05-11-2009
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.

HTML Code:
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

Last edited by thepurple; 05-11-2009 at 02:14 PM..
# 2  
Old 05-11-2009
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)

Code:
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[i] <= $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:

Code:
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[i] <= v && v <= t[i+1]) {
	    print v, "is in", g; break
		}
	    }
	}
	' range_file data_file


Last edited by radoulov; 05-11-2009 at 05:16 PM..
# 3  
Old 05-12-2009
Quote:
Originally Posted by radoulov
And what should be the expected output given your sample data?
Output will be simple.

935303455 groupxxxx
935479875 groupxxxx
935477987 groupxxxx
937499000 groupxxxx
# 4  
Old 05-12-2009
In your sample there are no matching groups for the numbers in your example data, that's what I mean ...
# 5  
Old 05-13-2009
Quote:
Originally Posted by radoulov
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.
# 6  
Old 05-13-2009
Try the below code :

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 :

Code:
 
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

# 7  
Old 05-13-2009
if you have Python
Code:
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
Code:
# ./test.py
Group0324 --------> 935303
Group2006 --------> 935479
Group2006 --------> 935477
Group0324 --------> 937499
Group0324 --------> 937492
Group0404 --------> 937860
Group0324 --------> 935393

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read a file with decimal numbers in bash

Hello, I have the following script while read id fraction do sambamba -h -f bam -t 10 --subsampling-seed=50 -s $frac ${id}.bam -o ${id}.out.bam done < fraction.txt where fraction.txt has two columns (id,fraction) and 50 rows I am unable to run this as bash is not able to read the second... (2 Replies)
Discussion started by: nans
2 Replies

2. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

3. Shell Programming and Scripting

Match on a range of numbers

Hi, I'm trying to match a filename that could be called anything from vout001 to vout252 and was trying to do a small test but I'm not getting the result I thought I would.. Can some one tell me what I'm doing wrong? *****@********>echo $mynumber ... (4 Replies)
Discussion started by: Jazmania
4 Replies

4. Shell Programming and Scripting

Read numbers from file

I am trying to make a script to read marks from a file then find out how many of them are above 40 (passing marks). However my script is getting stuck at "read num". I dont understand whats the problem. Any help will be much appreciated. #!/bin/bash set -x count=0; countP=0; PASSMK=40... (6 Replies)
Discussion started by: vish6251
6 Replies

5. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

6. UNIX for Dummies Questions & Answers

How to count how many numbers in a certain range?

Hi I have a data file with two columns which looks like: 1 42 2 40 3 55 4 50 5 38 6 49 7 33 8 46 9 39 10 33 11 33 12 26 13 46 14 44 15 55 16 54 17 30 18 32 (7 Replies)
Discussion started by: marhuu
7 Replies

7. UNIX for Dummies Questions & Answers

Frequency of a range of numbers

Hello, I have a column where there are values from 1 to 150. I want to get the frequency of values in the following ranges: 1-5 6-10 11-15 .... .... .... 146-150 How can I do this in a for loop? Thanks, Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

8. Shell Programming and Scripting

Help with addition of 2 numbers that are read from a file

I am trying to add free and used memory (so that i can compute percentage used)of remote nodes using shell script. I use the openssh-server,expect tool and ssh script. 1)login.txt (info of nodes): ip1|username|password ip2|username|password . . . 3)sshlogin.sh #!/bin/bash ... (1 Reply)
Discussion started by: marmik1903
1 Replies

9. Shell Programming and Scripting

Inserting a range of consecutive numbers into a text file

I have a text file in the following format .... START 1,1 2,1 3,1 .. .. 9,1 10,1 END .... I want to change to the output to .... START 1,1 2,1 3,1 .. (4 Replies)
Discussion started by: VNR
4 Replies

10. UNIX for Dummies Questions & Answers

Using grep on a range of numbers

Hi im new to unix and need to find a way to grep the top 5 numbers in a file and put them into another file. For example my file looks like this abcdef 50000 abcdef 45000 abcdef 40000 abcdef 35000 abcdef 30000 abcdef 25000 abcdef 20000 abcdef 15000 abcdef 10000 and so on... How can... (1 Reply)
Discussion started by: ProgChick2oo9
1 Replies
Login or Register to Ask a Question