Adding Zeros to make it 8 digit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding Zeros to make it 8 digit
# 1  
Old 02-28-2012
Adding Zeros to make it 8 digit

Dear Buddies,
My query may be pretty simple but I am stuck because of it. Please give me a solution.
Query is I have a variable (say $A) which can have any number starting from 1 digit to 8 digit i.e. any number from 1 to 99999999.
Now, I want output as 8 digit even if variable has only 1 digit. IT can be accomplished by addting Zeros (0) before the value of $A.

E.g 1)

$A=1
then output should be printed as 00000001

E.g 2)
$A=1234
then output should be printed as 00001234

E.g. 3)
$A=1234567
then output should be printed as 01234567

E.g 4)
$A=12345678
then output should be printed as 12345678 (As it is because it is already 8 digit number)

Last edited by fpmurphy; 02-28-2012 at 10:17 PM.. Reason: remove colors and fonts
# 2  
Old 02-28-2012
Quote:
Originally Posted by anushree.a

$A=1234
then output should be printed as
00001234
$A=1234. This would be interpreted as a command and not as an assignment.

You could do this:
Code:
A=1234
printf "%08d" $A

Or you could also try searching the forum for previous posts of similar kind.

Last edited by balajesuri; 02-28-2012 at 02:39 AM.. Reason: Typo error.. printf "%08d" and not printf "%010d"
# 3  
Old 02-28-2012
Code:
A=1234 
printf "%08d" $A

# 4  
Old 02-28-2012
If you are using ksh93, you can use typeset -Z, i.e.
Code:
$ typeset -Z8 var=123
$ echo $var
00000123

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding Leading Zeros for date in a file

Hello, I have a pipe separated file with two major lines. One is header and another is detail line. Header starts with H and Detail start with D. Sample Content: H|123456|Joes Watson|UK|4/5/2016|12/5/2016|3456|HC|NW|||||| D|123456|Joes... (13 Replies)
Discussion started by: Mannu2525
13 Replies

2. Shell Programming and Scripting

Fixed with file- removing leading zeros and adding the space

Hi All, i have a fixed width file , where each line is 3200 length. File: 1ABC 1111 2222 3333 000012341 1001 2ABC 1111 2222 3333 000012342 1002 3ABC 1111 2222 3333 000112343 1003 1DEF 5555 4444 9696 000012344 1004 2DEF 5555 2323 8686 000012345 1005 3DEF 5555 1212 7676 000012346 1006 ... (1 Reply)
Discussion started by: mechvijays
1 Replies

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

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

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

6. Shell Programming and Scripting

Help with adding leading zeros to a filename

Hi i need help in adding leading zero to filenames e.g file name in my folder are 1_234sd.txt 23_234sd.txt the output i need is 001_234sd.txt 023_234sd.txt can i do this shell scripting please help (2 Replies)
Discussion started by: rsmpk
2 Replies

7. Shell Programming and Scripting

Trim leading zeros to make field 6 characters long

Hi all- I've got a file that will have multiple columns. In one column there will be a string that is 10 digits in length, but I need to trim the first four zeros to make it 6 characters? example: 0000001234 0000123456 0000234566 0000000321 output: 001234 123456 234566 000321 (5 Replies)
Discussion started by: Cailet
5 Replies

8. Shell Programming and Scripting

using awk make an field as 5 digit and display

using awk convert 3 rd fileld of file as 5 digit and then display changed file. like 1 2 23445 3452 3343 3 5 6 6 ================ o/p:- 1 2 23445 3452 03343 3 5 00006 6 (1 Reply)
Discussion started by: RahulJoshi
1 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

adding a 6 digit number retaining 0s on the left

i am new to shell scripting. i want to keep on increamenting a 6 digit number. For eg. 000000 + 1 = 000001 But instead of 000001 i get only 1. How do i do this ? Pls help. (8 Replies)
Discussion started by: kanchan_cp
8 Replies
Login or Register to Ask a Question