Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add variable preceding numbers

Status
Not open for further replies.

Dominal

Technical User
Mar 2, 2003
14
HK
Hello!

I have a file which stores a number between 1~9999999. However, if the number is less than 7 digits, I need to fill it with 0s. e.g. if the number is 14, then I need to make it be 0000014. Any hints?

Thanks!
 
Ygor, sorry for misleading. What I mean is, the value in file1 is unknown, or better say it changes everyday. If file1 contains 14, we need to add 5 preceding 0s to 0000014. If file1 contains 8682, we need to add 3 0s to 0008682.

My approach is to add seven 0s to the number in file1 which yields [0000000<1~7 digit number>], and cut the right hand side 7 digits to [<(7-no. of digits in file1) 0s><number in file1>].

I tried your method printf &quot;%07d\n&quot; `cat file1` but it doesn't work. Thanks a lot!
 
What doesn't work with
[tt]printf &quot;%07d\n&quot; `cat file1`[/tt]
? //Daniel
 
Ygor & DanielHozac,

It works! Thanks for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top