Digits with starting zero's in loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Digits with starting zero's in loops
# 1  
Old 11-30-2013
Digits with starting zero's in loops

Hi friends,

I have 2 requirements on numbers with starting zero's to make them having equal number of digits.

1. To setup a for loop for 1-100, it can be done by
Code:
for ((i=1;i<=100;i++))
do 
echo $i 
done

but I need to setup them like 001,002,003,004 ...100. How can I achieve this?


2. I am increasing the count on one variable by looping from another variable like
Code:
cnt=001
for fil in *
do
blah blah...
blah blah..
cnt=`expr $cnt +1`
done

In the above loop I need the values of cnt as 001,002,003 etc...

Please advise how can I achieve them, thanks a lot!!
# 2  
Old 11-30-2013
Code:
printf "%03d\n" $i

or do it with the seq command if installed.
Code:
seq -w 1 100

# 3  
Old 11-30-2013
Hi.
Minor alteration to code from frank_rizzo:
Code:
printf "%03d\n" {1..10}

producing:
Code:
001
002
003
004
005
006
007
008
009
010

Best wishes ... cheers, drl
# 4  
Old 12-01-2013
not very portable but bash4 does for i in {001..100} with zero-padding.
you can also use printf -v to save a subshell.

Code:
  for ((i=0;i<=100;i++));do
    printf -v j '%03d' "$i"
    echo "$j"
  done

# 5  
Old 12-02-2013
You could also (depending on your shell, which you haven't told us, but it looks like bash)
Code:
typeset -Z3 i

This will set the value to be three digits with leading zeros as required. As a caution though, it will use the right-most three digits it your value is higher.

This can be useful sometimes, but can get in the way:-
Code:
$ typeset -Z3 i=1234
$ echo $i
234



I hope that this helps,
Robin
Liverpool/Blackburn
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Showing 4 digits

Hello everybody I'm a little beginer for shell script as I started last night... I have this script cat fichier.txt | while read l ; do #echo $l echo $x x=$(( $x + 1 )) done it's return 1 2 3 4 (4 Replies)
Discussion started by: remibemol
4 Replies

3. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

4. Programming

6 digits combination

Is there any program that can create 6 digit numbers with: (DIGIT_1)+(DIGIT_2)+(DIGIT_3)+(DIGIT_4)+(DIGIT_5)+(DIGIT_6)=10 Any perl or C also can. Anyone can help me? Thank you (6 Replies)
Discussion started by: Tzeronone
6 Replies

5. UNIX for Advanced & Expert Users

How to replace last 8 digits?

Hi, How I can replace last 8 ZEROS with 22991231? 19523479811841494432A2013052700000000 19523479811730333980A2013052700000000 19523479811417044397A2013052700000000 19523479811205895810C2013010120130131 A9523479811205895810A2013020120130228 19523479811205895810I2013030120130331... (9 Replies)
Discussion started by: jnrohit2k
9 Replies

6. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

7. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

8. Shell Programming and Scripting

total last digits

hi group, How can I count total number of 5's which are continuous in the end. i.e. in the below string, the o/p should be 4 I just know to calculate total number of 5's $ echo "95952325555" | awk -F "5" '{print NF-1}' 6 (3 Replies)
Discussion started by: uwork72
3 Replies

9. Shell Programming and Scripting

Formatting digits

I want to check the argument in KSH. If the user type in the prompt 'find 3' it will format 3 to 003 to match the data in the text file. Same as with 10 to 010. Always begins with 0. eg. >find 3 Output: 003 >find 30 Output: 030 (7 Replies)
Discussion started by: harry0013
7 Replies

10. UNIX for Dummies Questions & Answers

Only Digits as input

Hi All, I am new to shell script. I wrote a very small script that takes only digits as input- but there is some problem in that.can you help me in debugging that. #!/bin/ksh echo "Digits as input" read number digit='eval ' if ] then echo "Entered number is a digit" else echo... (2 Replies)
Discussion started by: namishtiwari
2 Replies
Login or Register to Ask a Question