Need advise to generate 10 character numbers example 0000000000 - 9999999999


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need advise to generate 10 character numbers example 0000000000 - 9999999999
# 1  
Old 07-15-2013
Need advise to generate 10 character numbers example 0000000000 - 9999999999

Hi all, i need advise from all experts here.
when i use command below to print number in 10 character

Code:
printf "%010d\n" {0..999}

it will give me output nicely
starts from
Code:
0000000000

untillllllllllll
Code:
0000000994
0000000995
0000000996
0000000997
0000000998
0000000999

However. when i put like this
Code:
printf "%010d\n" {0..9999999999}

sadly , it wont run SmilieSmilieSmilie and my window just close. "poopssSmilieSmilie" i guess printf have limits and not able to handle such request and terminate.

i want to able to print 10 character number output
from
0000000000
untilll
9999999999

maybe "awk" able to do it ?? but i stumble until now because i dont know how to make it to run from 0 untill 9999999999

i only managed to solve until
Code:
awk '{printf("%010d\n"),$1}'

which can print 10 zeros. but i dont know how to make it print my desire outputSmilieSmilie

please help me.

then i can save it into a file.

Last edited by Scrutinizer; 07-15-2013 at 01:41 PM.. Reason: code tags
# 2  
Old 07-15-2013
It's not printf's limit.

When you do that, you are not looping. You are telling the shell, "calculate every possible number between 000 and 999, then print them".

When you do so for all numbers between 000000000 and 9999999999, it runs out of memory.

To do an actual loop:

Code:
for ((X=0; X<10000; X++))
do
        printf "%010d\n" $X
done > filetosaveto

# 3  
Old 07-15-2013
Linux has seq
Code:
seq -f %010.0f 0 9999999999

You can pipe this into a while loop.
On Unix systems you can do it with awk
Code:
awk 'BEGIN {for (i=0;i<=9999999999;i++) printf "%010.0f\n",i}'


Last edited by MadeInGermany; 07-15-2013 at 01:36 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-17-2013
Hi MadeInGermany

thanks, your code working great like charm Smilie

btw, do you mind to explain what your code do. i like to learn more
Code:
awk 'BEGIN {for (i=0;i<=9999999999;i++) printf "%010.0f\n",i}'

Hope i not trouble you again and please explain to me how your code actually works.?

Thanl You Smilie

Last edited by Scott; 07-17-2013 at 12:18 PM.. Reason: Code tags
# 5  
Old 07-17-2013
The code is pretty much straightforward:
Code:
awk '
        BEGIN {
                for ( i = 0; i <= 9999999999; i++ )
                {
                        printf "%010.0f\n",i
                }
        }
'

For reference:

BEGIN Rule

For Statement

Printf Statement
This User Gave Thanks to Yoda For This Post:
# 6  
Old 07-17-2013
Two additional notes:
  1. awk is faster than shell (and even little faster than perl)
  2. nawk has an overflow bug with %010d therefore the %010.0f (and seq only takes the latter)
# 7  
Old 07-17-2013
Quote:
Originally Posted by MadeInGermany
Two additional notes:
  1. awk is faster than shell (and even little faster than perl)
  2. nawk has an overflow bug with %010d therefore the %010.0f (and seq only takes the latter)
%d expects an int argument. Even on most 64 bit architectures, int is 32 bits and not capable of spanning all 10 digit decimal integers.

I don't consider that int overflow a bug. Instead, I consider any support beyond the int range an extension.

POSIX requires AWK implementations to represent all numbers as doubles. This makes it convenient for gawk to implement %d with %.0f.

From gawk 4.1 stable :: builtin.c:
Code:
			while ((i = snprintf(cpbufs[1].buf,
					     cpbufs[1].bufsize, "%.0f",
					     tmpval)) >=
			       cpbufs[1].bufsize) {

nawk casts the double to a long.
Code:
		case 'd':	snprintf(p, buf + bufsize - p, fmt, (long) getfval(x)); break;

However, there's nothing in the standard that requires %d to support anything beyond int.

Regards,
Alister

Last edited by alister; 07-17-2013 at 09:25 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate sequence of numbers

I need awk script to generate part number sequencing based on data in multiple columns like below Input File --------- Col A|Col B|Col C| 1|a|x| 2|b|y| |c|z| | |m| | |n| And out put should be like 1ax 1ay 1az 1am 1an 1bx 1by (6 Replies)
Discussion started by: aramacha
6 Replies

2. Shell Programming and Scripting

Auto generate Line Numbers

How do I generate line numbers in Vi? I have this: ,'04-90020-039N','61423','2GDV00039-0002', SYSDATE); ,'04-90020-040D','61423','2GDV00046-0001', SYSDATE); ,'04-90020-041N','61423','2GDV00038-0002', SYSDATE); ,'04-90020-043D','61423','2GDV00047-0001', SYSDATE);... (3 Replies)
Discussion started by: djehresmann
3 Replies

3. Shell Programming and Scripting

Generate random numbers in script

i want to generate a random number through a script, and even if anyone reads the script, they wont be able to figure out what the random number is. only the person who setup the script would know it. something like this could work: random the full thread is here: ... (13 Replies)
Discussion started by: SkySmart
13 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

Generate Codes based on start and End values of numbers in a column

Hello All, Could you please help with this. This is what I have: 506234.222 2 506234.222 2 506234.222 2 506234.222 2 508212.200 2 508212.200 2 333456.111 2 333456.111 2 333456.111 2 333456.111 2 But this is what I want: 506234.222 1 506234.222 2 506234.222 2 506234.222 3 (5 Replies)
Discussion started by: canimba
5 Replies

6. Programming

generate array of random numbers

hi guys, I am writing a c program that generates a two dimensional array to make matrix and a vector of random numbers and perform multiplication. I can't figure out whats wrong with my code. It generates a matrix of random numbers but all the numbers in the vector array is same and so is the... (2 Replies)
Discussion started by: saboture88
2 Replies

7. Shell Programming and Scripting

Generate numbers 000 to 999

I have tried to make this script to generate: 000 001 002 ... 997 998 999 i=0 while do if then echo "00"$i else if && then echo "0"$i (5 Replies)
Discussion started by: locoroco
5 Replies

8. Shell Programming and Scripting

How to generate 10.000 unique numbers?

hello, does anybody can give me a hint on how to generate a lot of numbers which are not identically via scripting etc? (7 Replies)
Discussion started by: xrays
7 Replies

9. Shell Programming and Scripting

How to generate a series of numbers

Hi All, I have a requirement where in I have an input as follows:- input=1-4,6,8-10,12-15 I need to explode this range into an output file as follows:- 1 2 3 4 6 8 9 10 12 13 14 15 My input may vary like 1,5-9,11-13,15-17....... (3 Replies)
Discussion started by: rony_daniel
3 Replies

10. Shell Programming and Scripting

generate level numbers

Hi... I have a sequence of jobs and its predecessors.. Input Job_Name Predecessor A NULL B1 A B2 A B3 B1 C B3 C B2 So based on these i have to generate the level Number What i mean is Let A be level 1 for B1 to happen it should have done A so B1 level is A+1 = 1+1 = 2 (12 Replies)
Discussion started by: pbsrinivas
12 Replies
Login or Register to Ask a Question