Divide numbers into intervals


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Divide numbers into intervals
# 1  
Old 08-24-2010
Divide numbers into intervals

divide input values into specified number (-100 or -200) according to the key (a1 or a2 ....)
For ex: if we give -100 in the command line it would create 100 number intervals (1-100, 100-200, 200-300) untill it covers the value 300 in a1.

Note: It should work the same even with huge numbers 239349456667 with huge limit( -10000000 )out having the float number problem.

Thanx a billion!!!!

input

Code:
a1	300
a2	400
a3	198
a4	312

Code:
./script input -100 >>output1

output1

Code:
a1	1	100
a1	100	200
a1	200	300
a2	1	100
a2	100	200
a2	200	300
a2	300	400
a3	1	100
a3	100	200
a4	100	200
a4	200	300
a4	300	400

Code:
./script input -200 >>output2

output2

Code:
a1	1	200
a1	200	400
a2	1	200
a2	200	400
a3	1	200
a4	1	200
a4	200	400

# 2  
Old 08-24-2010
Code:
perl -lane'BEGIN {
  $l = shift; $, = "\t";
  }
print $_, "out of range" and next 
  if $F[1] < $l;
while (1) {  
  last if (($i += $l) + $l) > $F[1];
  print $F[0], $i ? $i : 1, $i + $l;
  }' 100 infile

# 3  
Old 08-24-2010
Quote:
Originally Posted by radoulov
Code:
perl -lane'BEGIN {
  $l = shift; $, = "\t";
  }
print $_, "out of range" and next 
  if $F[1] < $l;
while (1) {  
  last if (($i += $l) + $l) > $F[1];
  print $F[0], $i ? $i : 1, $i + $l;
  }' 100 infile


The code is not giving the desired o/p see below

Code:
o/p
a1      100     200
a1      200     300

BR

---------- Post updated at 14:21 ---------- Previous update was at 14:13 ----------

radoulov, you forget to reset the $i counter.

after modification on your code, it worked , see below:-


Code:
 perl -lane'BEGIN {
  $l = shift; $, = "\t";
  }
print $_, "out of range" and next 
  if $F[1] < $l;
while (1) {  
  last if ($i + $l) > $F[1];        
  print $F[0], $i ? $i : 1, $i+$l; $i+=$l ; 
  } ; $i=0 ;' 100 test1

# 4  
Old 08-24-2010
Quote:
Originally Posted by ahmad.diab
The code is not giving the desired o/p see below

Code:
o/p
a1      100     200
a1      200     300

BR
Yep,
sorry:
Code:
perl -lane'BEGIN {
  $l = shift; $, = "\t";
  }
print $_, "out of range" and next 
  if $F[1] < $l;
for ($i = 0; $i < $F[1]; $i += $l) {  
  print $F[0], $i ? $i : 1, $i + $l;
  }' 100 infile



---------- Post updated at 01:25 PM ---------- Previous update was at 01:21 PM ----------

With awk:

Code:
awk '{
  if (l > $2) {
    print $0, "out of range"; next
    }
  for (i = 0; i < $2; i += l)
    print $1, i ? i : 1, i + l 
  }' OFS='\t' l=100 infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Divide the numbers in file

Dear ALL, I have sample file : tx_bytes: 2422, tx_packets: 13, uptime: 16119, tx_bytes: 2342, tx_packets: 14, uptime: 11009, tx_bytes: 252, tx_packets: 12, uptime: 3113, my formula : minutes=$(( uptime/60%60 )) hours=$(( uptime/60/60%24 )) (3 Replies)
Discussion started by: gnulyn
3 Replies

2. UNIX for Dummies Questions & Answers

Gap length between intervals

hi all, I wish to calculate the length between intervals whose are defined by a starting and an end possition. The data looks like this: 1 10 23 30 45 60 70 100... The desired output should be: 13 # (23-10) 15 # (45-30) 10 # (70-60)... I donīt know how to operate with different... (2 Replies)
Discussion started by: lsantome
2 Replies

3. Shell Programming and Scripting

Script to divide/expand first digit to show some numbers

Hello to everyone, I have this complex problem and I don't how to do it. I'm not sure if awk could be a good choice to do it or could be easiest in bash or perl. A kind of introduction would be: - I have a digit, lets say 3. - I can expand/spread out the digit 3 to cover all possible... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

4. UNIX for Dummies Questions & Answers

Building intervals

Hi all, I hope you can help me with the following question: I have multiple tables like this: Chr Start End Zygosity Gene chr1 153233510 153233510 het LOR chr1 153233615 153233615 hom LOR chr1 153233701 153233701 hom LOR chr1 ... (5 Replies)
Discussion started by: lsantome
5 Replies

5. Shell Programming and Scripting

Add and divide each numbers with the added number

Hi All, I am stuck with this problem. I have some 100000 (.dat) 1.dat, 2.dat,3.dat etc until 100000.dat files which look like this: 1.dat 1 2 3 4 0.99 4.54 All my files 1.dat until 100000.dat look the same but with different numbers. I have to first add all the numbers in each... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

Grouping data numbers in a text file into prescribed intervals and count

I have a text file that contains numbers (listed from the smallest to the largest). For ex. 34 817 1145 1645 1759 1761 3368 3529 4311 4681 5187 5193 5199 5417 5682 . . (5 Replies)
Discussion started by: Lucky Ali
5 Replies

7. Programming

performing a task at regular intervals

hi! i m tryin to write a program that will perform a specific tasks after fixed interval of time.say every 1 min. i jus donno how to go abt it.. which functions to use and so on... i wud like to add that i am dont want to use crontab over here. ny lead is appreciated. thanx. (2 Replies)
Discussion started by: mridula
2 Replies

8. Shell Programming and Scripting

mailing myself at regular intervals...

hi all, i wrote a script to mail myself using pine (modified) to keep remind of b'days. #!/bin/bash grep "`date +%D |awk -F/ '{print $2+1, $1+0}'`" dataFile >/home/username/mailme if test -s /home/username/mailme then pine -I '^X,y' -subject "Birthday Remainder" username... (4 Replies)
Discussion started by: timepassman
4 Replies

9. Shell Programming and Scripting

Date Intervals

I posted a question on date intervals about a month back asking about how I could be able to go about a user entering the starting year/month/day and an ending year/month/day and then the script automatically cycling through each day of each month of each year that the user has specified. I... (7 Replies)
Discussion started by: yongho
7 Replies

10. Shell Programming and Scripting

How to perform Date Intervals?

I have a 300 line script which generates key performance indicators for one of our systems. Since I just started learning sh/ksh half a month ago there's still a lot I haven't had experience with yet. Currently, the script generates the report for a specific day. (It takes the date specified by... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question