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:

Min-Range   Max Range
000000	    999999
100000	    199999
140000	    199999
300000	    699999

Desired output:

0-9
1
14
15
16
17
18
19
3
4
5
6

Thanks for any help.

Any attempts from your side?

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

Howsoever, try

awk '
NR > 1  {DELTA = $2 - $1
         n=split (DELTA, T, "")
         for (i=1; i<=n; i++) if (T == "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.

1 Like

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:

300000 309999
310000 319999
320000 320999
321000 321999
322000 322899

the output should be:

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

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.

The script suggested was created to accommodate that header line.
Try with that constrain removed.

awk '
        {DELTA = $2 - $1
         n=split (DELTA, T, "")
         for (i=1; i<=n; i++) if (T == "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
1 Like

Hi Aia,

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

Both versions it seems to work.

Regards

Another approach, svp? Try

awk '{TH=10^int(log($2 - $1)/log(10) + 0.005); for (i=int($1 / TH); i<=int($2 / TH); i++) print i}' file