Pad zeros to a number | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Pad zeros to a number

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-24-2007
anumkoshy anumkoshy is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 26 March 2010, 6:58 AM EDT
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Pad zeros to a number

Pad zeros to a number and assign it to a variable


like i get 1 in $i ,i want it to be $i as 01
Sponsored Links
    #2  
Old 08-24-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
 
Join Date: Aug 2007
Last Activity: 2 October 2012, 1:31 PM EDT
Location: Bangalore
Posts: 390
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by anumkoshy View Post
Pad zeros to a number and assign it to a variable


like i get 1 in $i ,i want it to be $i as 01

cat file1
1
2
3
4
5
6

cat file1 | sed 's/^/0/g'

01
02
03
04
-
--


Thanks
Namish
Sponsored Links
    #3  
Old 08-24-2007
anumkoshy anumkoshy is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 26 March 2010, 6:58 AM EDT
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
i lll explain it

any ways thanks for reply..........

i want to pad zero to a run time variable

ie
eg :$i is the variable

if $i =1 it should be like 01
if $i=11 it shoild be 11 itself
    #4  
Old 08-24-2007
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
 
Join Date: Apr 2002
Last Activity: 20 December 2009, 10:56 AM EST
Location: Italy
Posts: 370
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
i=5
i=`printf "%02s\n" "$i"`
echo "$i"

05

Sponsored Links
    #5  
Old 08-24-2007
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
If you use ksh, you can use typeset

Code:
[/tmp]$ cat test.ksh
#! /bin/ksh

typeset -RZ2 val

val=1
echo $val
val=11
echo $val
[vivarkey@/tmp]$ ./test.ksh 
01
11
[/tmp]$

Sponsored Links
    #6  
Old 08-24-2007
anumkoshy anumkoshy is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 26 March 2010, 6:58 AM EDT
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
thanks a lot

it worked
Sponsored Links
    #7  
Old 08-27-2007
manas_ranjan's Avatar
manas_ranjan manas_ranjan is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 17 November 2014, 5:53 AM EST
Location: Amsterdam
Posts: 273
Thanks: 12
Thanked 0 Times in 0 Posts
hey.....

u can also use

var=$1
case $var in
[0-9]) var=0$var
echo $var;;
*) echo $var;;
esac
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pad Zeros at the end greenworld123 Shell Programming and Scripting 11 12-14-2011 01:34 AM
awk to remove leading zeros for a hex number carloszhang Shell Programming and Scripting 4 04-01-2011 01:40 AM
How to right pad with zeros using sprintf? emitrax Programming 9 09-21-2010 02:22 PM
Removing Zeros in front of a number Raynon Shell Programming and Scripting 13 05-22-2008 05:02 AM
pad Zeros mgirinath UNIX for Dummies Questions & Answers 5 04-10-2007 11:22 AM



All times are GMT -4. The time now is 08:05 AM.