Script to divide/expand first digit to show some numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to divide/expand first digit to show some numbers
# 1  
Old 10-14-2014
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:
Code:
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
# 2  
Old 10-14-2014
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?
# 3  
Old 10-14-2014
Hello Corona,

Thanks for answer. I'll try to answer.

Quote:
Originally Posted by Corona688
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?
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).

Quote:
Originally Posted by Corona688
And the second one, why does it expand to 3350-3359 but not any other 4-digit ranges?
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.
Quote:
Originally Posted by Corona688
And where do the 2-digit ranges come from?
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.

Last edited by Ophiuchus; 10-14-2014 at 05:24 PM..
# 4  
Old 10-14-2014
Quote:
Originally Posted by Ophiuchus
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.
Why does that matter when expanding a 4 digit number and not a 3 digit one?
Quote:
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.
The list will never represent all numbers that begin with 3. An exhaustive list will be infinite.
# 5  
Old 10-14-2014
How about this:

Code:
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)}'

# 6  
Old 10-14-2014
Hello Chubler_XL,


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

Quote:
Originally Posted by Corona688
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.
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.
Code:
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.
# 7  
Old 10-14-2014
Edit: Sorry just worked thru your 2nd example above, and I realize the solution I posted was incorrect.

Please try this

expand.sh:

Code:
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
Code:
$ 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


Last edited by Chubler_XL; 10-15-2014 at 12:49 AM..
This User Gave Thanks to Chubler_XL For This Post:
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. Shell Programming and Scripting

Using sed to show even numbers and the line

Hello, I have an assignment that requires us to use sed only. The first part of the assignment says to use a sed script to print only the even lines, but if it is print, the number of that lines needs to be printed before the text. ex: 2 line 2 text 4 line 4 text 6 line 6 text ... (3 Replies)
Discussion started by: stevent518
3 Replies

3. Shell Programming and Scripting

Divide by zero script

Hi, I've been working on a few scripts and have been getting great info. How, would I include in the below script, how I would let a user know that if they divide a SECOND number by zero, that they would get a divide by zero error? What's the easiest way of working this? Cordially, joe. ... (1 Reply)
Discussion started by: jefferj54
1 Replies

4. Shell Programming and Scripting

Generate 16 digit positive random Numbers

Hi Unix Gurus, I have a requirement to generate positive random 16 and 13 digit numbers. Here is the script I have so far..... number=$RANDOM$RANDOM$RANDOM$RANDOM; let "number %= 10000000000000"; echo $number But sometimes it is generating negative numbers and also 15 digit... (8 Replies)
Discussion started by: scorpioraghu
8 Replies

5. Shell Programming and Scripting

convert two digit in to single digit...

Hi Guys. My Input: ABCD 12 00 KL ABCD 12 08 DL ABCD 12 10 KK ABCD 12 04 LL ABCD 13 00 LP ABCD 13 1O LS Output: ABCD 12 0 KL ABCD 12 8 DL ABCD 12 10 KK ABCD 12 4 LL ABCD 13 0 LP (2 Replies)
Discussion started by: pareshkp
2 Replies

6. Shell Programming and Scripting

awk length of digit and print at most right digit

Have columns with digits and strings like: input.txt 3840 3841 3842 Dav Thun Tax Cahn 146; Dav. 3855 3853 3861 3862 Dav Thun Tax 2780 Karl VI., 3873 3872 3872 Dav Thun Tax 3894 3893 3897 3899 Dav Thun Tax 403; Thun 282. 3958 3959 3960 Dav Thun Tax 3972 3972 3972 3975 Dav Thun Tax... (8 Replies)
Discussion started by: sdf
8 Replies

7. 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

8. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: ruby_sgp
3 Replies

9. Shell Programming and Scripting

Replace one digit by two digit using sed

Folks, Is there a simple way to replace one digit by two digit using sed. Example, mydigit1918_2006_8_8_lag1.csv should be mydigit1918_2006_08_08_lag01.csv. I tried this way, but doesn't work. echo mydigit1989_2006_8_8_lag1.csv|sed 's/]/0]/' Thank you, (5 Replies)
Discussion started by: Jae
5 Replies

10. Shell Programming and Scripting

script needs to divide by 2

i need to divide this count by 2, what variable can i use in my script? 26 hcscprod_cpus_totals /2 = 13 13 hcncprod_cpus_totals /2= 6.5 541 ktazp_cpus_totals /2= 270.5 346 ktazd_cpus_totals /2=173 110 ktazi_cpus_totals /2=55 10 ktazq_cpus_totals /2=5 (2 Replies)
Discussion started by: wereyou
2 Replies
Login or Register to Ask a Question