Print root number between min and max ranges


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print root number between min and max ranges
# 1  
Old 11-13-2015
Print root number between min and max ranges

Hi to all,

Please help on the following problem, I'm not where to begin, if awk or shell script.

I have pairs of ranges of numbers and I need to find the root or roots of ranges based on min Range and Max ranges

Example #1:
If min range is 120000 and max ranges 124999, it means that are contained numbers of the form 12XYYY, where X= 0-4 and Y=0-9,
then the roots for this pair of ranges are 120, 121, 122, 123 and 124 since between those ranges only will be numbers that begin with that
roots.

Example #2:
If min range = 200000 and max range = 299999, then it means that the numbers are of the form 2XXXXX where X=0-9 and there is only one root
that is 2

Example #3:
If min range = 570000 and max range = 579999, then it means that the numbers are of the form 57XXXX where X=0-9 and there is only one root
that is 57

The input is:
Code:
Min-Range   Max Range
000000	    999999
100000	    199999
140000	    199999
300000	    699999

Desired output:
Code:
0-9
1
14
15
16
17
18
19
3
4
5
6

Thanks for any help.
# 2  
Old 11-14-2015
Any attempts from your side?

---------- Post updated at 12:32 ---------- Previous update was at 10:54 ----------

Howsoever, try
Code:
awk '
NR > 1  {DELTA = $2 - $1
         n=split (DELTA, T, "")
         for (i=1; i<=n; i++) if (T[i] == "9") break
         B = 0 + substr ($1, 1, length($1) - length(DELTA) + i - 1)
         E = 0 + substr ($2, 1, length($2) - length(DELTA) + i - 1)
         for (i=B; i<=E; i++) print i
        }
' file
0
1
14
15
16
17
18
19
3
4
5
6
120
121
122
123
124
2
57

, including your examples #1 - #3 into the range file.

---------- Post updated at 12:46 ---------- Previous update was at 12:32 ----------

Handling of the first range line (field 1 being "000000") needs some special treatment; insert if (substr($1,1,1)=="0") {print 0; $1="1" substr($1,2)} just before DELTA is calculated.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-14-2015
Hi RudiC,

Thanks so much for you help. I've tried your code and I think I can handle the input in order to avoid ranges beginning with 0, so the extra code before DELTA is not needed to make it simpler.

The code almost work, since if the input is:
Code:
300000 309999
310000 319999
320000 320999
321000 321999
322000 322899

the output should be:
Code:
30
31
320
321
3220
3221
3222
3223
3224
3225
3226
3227
3228

But in the output I get the first root (30) is not printed. Only prints from 31 to 3228.

Thanks for help so far.

Best regards
# 4  
Old 11-14-2015
Your input files are not consistent.

Your original input file has a header line with Min-Range Max Range
Quote:
Min-Range Max Range
000000 999999
100000 199999
140000 199999
300000 699999
The second input file you are complaining about does not have that header line.
Quote:
300000 309999
310000 319999
320000 320999
321000 321999
322000 322899
The script suggested was created to accommodate that header line.
Try with that constrain removed.
Code:
awk '
        {DELTA = $2 - $1
         n=split (DELTA, T, "")
         for (i=1; i<=n; i++) if (T[i] == "9") break
         B = 0 + substr ($1, 1, length($1) - length(DELTA) + i - 1)
         E = 0 + substr ($2, 1, length($2) - length(DELTA) + i - 1)
         for (i=B; i<=E; i++) print i
        }
' file

This User Gave Thanks to Aia For This Post:
# 5  
Old 11-14-2015
Quote:
Originally Posted by Aia
Your input files are not consistent.

Your original input file has a header line with Min-Range Max Range


The second input file you are complaining about does not have that header line.
Hi Aia,

You´re correct. Thank you, it was my error.

Both versions it seems to work.

Regards
# 6  
Old 11-15-2015
Another approach, svp? Try
Code:
awk '{TH=10^int(log($2 - $1)/log(10) + 0.005); for (i=int($1 / TH); i<=int($2 / TH); i++) print i}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print a row with the max number in a column

Hello, I have this table: chr1_16857_17742 - chr1 17369 17436 "ENST00000619216.1"; "MIR6859-1"; - 67 chr1_16857_17742 - chr1 14404 29570 "ENST00000488147.1"; "WASH7P"; - 885 chr1_16857_18061 - chr1 ... (5 Replies)
Discussion started by: coppuca
5 Replies

2. Programming

Php number array from max, min, step size mysql data

I want to create a form with data values in a dropdown list. The values in the dropdown list need to be generated on the fly from max, min and increment values contained in a mysql database. Hopefully this makes sense, I really have no idea where to start :confused: Thanks (6 Replies)
Discussion started by: barrydocks
6 Replies

3. Shell Programming and Scripting

Number of elements, average value, min & max from a list of numbers using awk

Hi all, I have a list of numbers. I need an awk command to find out the numbers of elements (number of numbers, sort to speak), the average value the min and max value. Reading the list only once, with awk. Any ideas? Thanks! (5 Replies)
Discussion started by: black_fender
5 Replies

4. Shell Programming and Scripting

Print numbers between two number ranges

Hi, I have a list.txt file with number ranges and want to print/save new all.txt file with all the numbers and between the numbers. == list.txt == 65936 65938 65942 && 65943 65945 ... (7 Replies)
Discussion started by: AK47
7 Replies

5. Shell Programming and Scripting

Print min and max value from two column

Dear All, I have data like this, input: 1254 10125 1254 10126 1254 10127 1254 10128 1254 10129 1255 10130 1255 10131 1255 10132 1255 10133 1256 10134 1256 10135 1256 10137... (3 Replies)
Discussion started by: aksin
3 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Print a line using a max and a min values of different columns

Hi guys, I already search on the forum but i can't solve this on my own. I have a lot of files like this: And i need to print the line with the maximum value in last column but if the value is the same (2 in this exemple for the 3 last lines) i need get the line with the minimum value in... (4 Replies)
Discussion started by: MetaBolic0
4 Replies

7. Shell Programming and Scripting

print max number of 2 columns - awk

Is it possible to print max number of 2 columns - awk note: print max if the integer is positive and print min if the integer is negative input a 1 2 b 3 4 c 5 1 d -3 -5 d -5 -3 output a 2 b 4 c 5 d -5 d -5 (4 Replies)
Discussion started by: quincyjones
4 Replies

8. Shell Programming and Scripting

get min, max and average value

hi! i have a file like the attachement. I'd like to get for each line the min, max and average values. (there is 255 values for each line) how can i get that ? i try this, is it right? BEGIN {FS = ","; OFS = ";";max=0;min=0;moy=0;total=0;freq=890} $0 !~ /Trace1:/ { ... (1 Reply)
Discussion started by: riderman
1 Replies

9. Shell Programming and Scripting

min and max value of process id

We are running a AIX 5.2 OS. Would anyone happen to know what the max value for a process id could be? Thanks jerardfjay :) (0 Replies)
Discussion started by: jerardfjay
0 Replies

10. Shell Programming and Scripting

awk to print mon and max values of ranges

HI all I'm trying to write an awk script to print the min and max value in a range(s) contained in another file - the range values are in $2 EG 114,7964,1,y,y,n 114,7965,1,y,y,n 114,7966,1,y,y,n 114,7967,1,y,y,n 114,7969,1,y,y,n 114,7970,1,y,y,n 114,7971,1,y,y,n 114,7972,1,y,y,n... (3 Replies)
Discussion started by: Mudshark
3 Replies
Login or Register to Ask a Question