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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 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 Unix and Linux 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 Unix and Linux 08-24-2007
robotronic's Unix or Linux Image
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 Unix and Linux 08-24-2007
vino's Unix or Linux Image
vino vino is online now Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 5 March 2015, 5:26 PM EST
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 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 Unix and Linux 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 Unix or Linux Image
Sponsored Links
    #7  
Old Unix and Linux 08-27-2007
manas_ranjan's Unix or Linux Image
manas_ranjan manas_ranjan is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 25 February 2015, 9:46 AM EST
Location: Amsterdam
Posts: 277
Thanks: 13
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 06:30 PM.