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 numbers that begin with 3 representing it with 2 digits and divide it in
    10 parts like this:
    30, 31, 32, 33, 34, 35, 36, 37, 38, 39. These 10 series are 3X, where X=0-9 and cover all values that begin with 3.

Well, what I really need is:
If have the digit 3 that covers all possible values that begin with 3 and I have some given numbers (one or more) that begin
with the same digit(in this example the digit is 3). I need to spread out the digit 3 up to the number of digits needed depending the
number of digits of the given numbers. Then add the text "GivenNum" once the 3 has been divided to show the given numbers.

Example:
Lets say that initial digit is 3 and given numbers are only two (304 and 3357):
Initial digit 3:
Numbers to show: 304, 3357

The output I'm looking for would be:

300
301
302
303
304 GivenNum
305
306
307
308
309
31
32
330
331
332
333
334
3350
3351
3352
3353
3354
3355
3356
3357 GivenNum
3358
3359
336
337
338
339
34
35
36
37
38
39
  • As you can see for the first number 304, is needed to expand the series that begin with 3 up to 3 digits, since 304 has 3 digits.
  • For the second number 3357, is needed to expand the series that begin with 3 up to 4 digits, since 3357 has 4 digits
  • The series 31, 32, 34, 35, 36, 37, 38, 39 remain only represented up to 2 digits since the given numbers are not within of them.

I hope make sense.

Thanks in advance for any help.

Regards

I'm afraid you've lost me.

Why does 304 expand to 300-309, but not 310-319 or 320-329? After skipping those two it does expand again -- to 330-334?

And the second one, why does it expand to 3350-3359 but not any other 4-digit ranges? And where do the 2-digit ranges come from?

Hello Corona,

Thanks for answer. I'll try to answer.

304 is only 3 digits length:
First digit=3
Second digit=0

Then I need to expand only the range that contains the given number, this is expand the 30X, where X=0-9. 31X and 32X are no expanded
since the goal is to show the output the most compact as possible and only expand as needed to show the given numbers (in this case 304, 3357).

3357 is only 4 digits length:
First digit=3
Second digit=3
Third digit=5

Then I need to expand/open only the series 335X. And to expand 335X, first I need to expand the range 33X since 335X is inside 33X.

The 2-digit ranges should be present if the given numbers are not inside them, since the range 3X is being expanded and the expansion should
contain all numbers that begin with 3 once is expanded. For example, if I only expand the series 30X and 335X without showing 38X, 39X, etc,
the output list won't be representing all numbers that begin with 3.

Thanks again.

Why does that matter when expanding a 4 digit number and not a 3 digit one?

The list will never represent all numbers that begin with 3. An exhaustive list will be infinite.

How about this:

awk '
function exp_str(val,first,s,i)
{
   if(val+0<10) return
   s=substr(val,1,length(val)-1)
   exp_str(s,0)
   i=substr(val,length(val),1)
   l=first?10:i
   if (l+0)
       for(k=0;k<l;k++)
           if(k==i) print s k " GivenNum"
           else print s k
}
{ exp_str($1,1)}'

Hello Chubler_XL,

How can I test the AWK code? jeje for example for the example of my first post?

Never will be an infinite list. I generate these lists from time to time but manually and is time consuming.

Is not a thing that 3 or 4 digits, only that expand or not the range needed, for example if the given numbers were
35 and 3703 the list would be.

30
31
32
33
34
35 GivenNum
36
3700
3701
3702
3703 GivenNum
3704
3705
3706
3707
3708
3709
371
372
373
374
375
376
377
378
379
38
39

Thanks again.

Edit: Sorry just worked thru your 2nd example above, and I realize the solution I posted was incorrect.

Please try this

expand.sh:

awk '
function expand(val,i,v)
{
   for(i=0;i<10;i++) {
      if(((val i) + 0) in vals) print val i " GivenNum"
      else {
         for(v in vals) {
            if( v ~ "^"val i) {
              expand(val i)
              break
            }
         }
         if( v !~ "^"val i) print val i
      }
   }
}
{ vals[$0] }
END {
    for(v in vals) {
       k=substr(v,1,1);
       if(!(k in done)) {
           expand(k)
           done[k]
       }
    }
}'

Calling it

$ printf "35\n3703\n" | ./expand.sh
30
31
32
33
34
35 GivenNum
36
3700
3701
3702
3703 GivenNum
3704
3705
3706
3707
3708
3709
371
372
373
374
375
376
377
378
379
38
39
1 Like

Hello Chubler_XL,

It seems to work just perfect!!!.

I've tried more than 2 "given numbers" and does the work too. I've tried this:

printf "300001237601\n35\n3703\n38457678\n38457675\n398\n39924" | ./expand.sh

I've been trying to understand how your function and END{} parts work, but I don't quite understand. May you explain me a little bit your code's logic please.

Thanks again for help so far.

Regards