tags:

views:

62

answers:

5

For example, I want to count from 001 to 100. Meaning the zero buffer would start off with 2, 1, then eventually 0 when it reaches 100 or more.

ex: 001 002 ... 010 011 ... 098 099 100

I could do this if the numbers had a predefined number of zeroes with printf "%02d" $i. But that's static and not dynamic and would not work in my example.

+2  A: 

If your system has it, try seq with the -w (--equal-width) option:

$ seq -s, -w 1 10
01,02,03,04,05,06,07,08,09,10

$ for i in `seq -w 95 105` ; do echo -n " $i" ; done
095 096 097 098 099 100 101 102 103 104 105
mobrule
This is a good idea but not exactly how i was envisioning it. It'll buffer with 3 zeroes if and only if it reaches an integer with 3 digits (ex: 100). If it reaches just 99, it'll only include a buffer with 2 zeroes. Sorry I wasn't specific.
readdit
@readdit that's what `seq` does
Johannes Schaub - litb
Johannes. Yes you are right. This method works correctly as well. I don't know what I was running yesterday.Thanks.
readdit
A: 

In Bash you can use brace expansion. Putting a 0 before either limit forces the numbers to be padded by zeros

echo {01..100} # 001 002 003 ...
echo {03..100..3} # 003 006 009 ...
Johannes Schaub - litb
That requires Bash 4. Earlier versions don't do padding in brace expansions.
Dennis Williamson
Thanks Johannes and Dennis. Yes, I found that this requires bash 4. Which i don't use. I found though I can use zsh in the same fashion. for i in $(zsh -c "echo {$FROM..$TO}")do echo $i;done Works fine and didn't have to script anything overly complicated.
readdit
@readdit: Calling zsh from a bash script seems rather wasteful. Either write the whole script in zsh (it has a high level of Bash compatibility, although some things are different enough that you'll have to make a few modifications) or use built in features of bash such as the printf technique I show in my answer. There are other techniques that will also work using only shell builtins.
Dennis Williamson
+2  A: 

If by static versus dynamic you mean that you'd like to be able to use a variable for the width, you can do this:

$ padtowidth=3
$ for i in 0 {8..11} {98..101}; do printf "%0*d\n" $padtowidth $i; done
000
008
009
010
011
098
099
100
101

The asterisk is replaced by the value of the variable it corresponds to in the argument list ($padtowidth in this case).

Otherwise, the only reason your example doesn't work is that you use "2" (perhaps as if it were the maximum padding to apply) when it should be "3" (as in my example) since that value is the resulting total width (not the pad-only width).

Dennis Williamson
Yes, you're probably right about using zsh in a bash script. I overlooked your method. Yours and SiegeX work great for what I need. Thanks.
readdit
+1  A: 
#!/bin/bash

max=100; 

for ((i=1;i<=$max;i++)); do 
    printf "%0*d\n" ${#max} $i
done

The code above will auto-pad your numbers with the correct number of 0's based upon how many digits the max/terminal value contains. All you need to do is change the max variable and it will handle the rest.

Examples:

max=10

01
02
03
04
05
06
07
08
09
10

max=100

001
002
003
004
005
006
...
097
098
099
100

max=1000

0001
0002
0003
0004
0005
0006
...
0997
0998
0999
1000
SiegeX
This version works as well. Thanks.
readdit
+1 - Even *more* dynamic!
Dennis Williamson
A: 
# jot is available on FreeBSD, Mac OS X, ...    
jot -s " " -w '%03d' 5   
jot -s " " -w '%03d' 10  
jot -s " " -w '%03d' 50  
jot -s " " -w '%03d' 100   
juan