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
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.
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)}'
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.