Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   Zero padding (http://www.programmingforums.org/showthread.php?t=12604)

Silicon_Pudding Feb 17th, 2007 11:47 AM

Zero padding
 
How can I add zero pad a number based on a variable length so that the length of the number is always the same?
Example:
00004
00023
34243
Always has a length of 5 with zeros added in when necessary

Edit: nm i found out how using seq -w

anyways, i have another question about %04d . Instead of using 4 for the digits, how could I make 4 be a variable instead? Would %0$(length)d work instead?

DaWei Feb 17th, 2007 12:12 PM

Just a suggestion: when you edit a post after finding a solution, please leave the original material. Other members may be looking for a solution to the same problem. Add your answer and help them out. Then add your new question, or start another thread.

Jimbo Feb 17th, 2007 12:14 PM

use printf %0nd $yourNum where n is the length you want displayed

Silicon_Pudding Feb 17th, 2007 12:21 PM

Quote:

Originally Posted by Jimbo (Post 124065)
use printf %0nd $yourNum where n is the length you want displayed

is n a variable? I don't want n to be predefined, but calculated at run time.

I apologize for deleting the original post

Jimbo Feb 17th, 2007 4:48 PM

I don't know if there's a log function in bash, but if you know the length of the longest one you could use printf %0${maxlen}d $yourNum.

Silicon_Pudding Feb 17th, 2007 11:12 PM

Thanks for the help. Normally I would test it, but I'm borrowing a friend's comp so no Linux. :( Just trying to do the best without


All times are GMT -5. The time now is 4:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC