Removing leading zeros from a variable | 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.

Removing leading zeros from a variable

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-27-2008
toshidas2000 toshidas2000 is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 27 February 2008, 11:17 AM EST
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Removing leading zeros from a variable

How do I remove or add leading zeroa from a variable. To make variable 10 characters long when adding zeros.
Sponsored Links
    #2  
Old 02-27-2008
DukeNuke2's Avatar
DukeNuke2 DukeNuke2 is offline Forum Staff  
Soulman
 
Join Date: Jul 2006
Last Activity: 28 July 2014, 2:09 AM EDT
Location: Berlin, Germany
Posts: 5,371
Thanks: 54
Thanked 229 Times in 222 Posts
have a look at the command "tr"...

hth,
DN2
Sponsored Links
    #3  
Old 02-27-2008
ranjithpr ranjithpr is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 20 August 2010, 3:34 AM EDT
Posts: 175
Thanks: 0
Thanked 3 Times in 3 Posts
You can use printf to add or remove leading zeros

$ printf "%05d\n" 5
00005

$ printf "%d\n" 00005
5

$ printf "%010d\n" 00005
0000000005
    #4  
Old 02-27-2008
toshidas2000 toshidas2000 is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 27 February 2008, 11:17 AM EST
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Nothing worked, I am using K Shell
Sponsored Links
    #5  
Old 02-27-2008
ranjithpr ranjithpr is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 20 August 2010, 3:34 AM EDT
Posts: 175
Thanks: 0
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by toshidas2000 View Post
Nothing worked, I am using K Shell
Nothing worked means, did you get any error or It didn't display anything?
Sponsored Links
    #6  
Old 02-27-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 5 January 2012, 7:47 AM EST
Location: India
Posts: 185
Thanks: 0
Thanked 5 Times in 3 Posts
A awk one to remove leading 0's


Code:
$ echo "0000000231" | awk '{print $1 + 0}'
231

The Following 3 Users Say Thank You to jaduks For This Useful Post:
Jotne (01-17-2012), Roga Danar (04-12-2013), sene_geet (10-16-2012)
Sponsored Links
    #7  
Old 02-27-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 10 January 2014, 5:37 AM EST
Location: Scotland
Posts: 594
Thanks: 1
Thanked 28 Times in 27 Posts
or:


Code:
 expr  "0000000231" + 0
231

The Following User Says Thank You to Tytalus For This Useful Post:
Roga Danar (04-12-2013)
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
Add leading zeros in floating point variable reeta_shri Shell Programming and Scripting 4 03-02-2011 06:02 AM
Removing leading zeros for a decimal column marpadga18 Shell Programming and Scripting 9 02-25-2011 11:11 PM
Removing leading spaces from the variable value. khedu Shell Programming and Scripting 3 09-28-2009 03:26 PM
Replacing leading zeros with leading spaces deepakwins UNIX for Dummies Questions & Answers 5 06-18-2008 02:06 AM
Leading zeros wtofu Shell Programming and Scripting 1 09-16-2006 02:52 PM



All times are GMT -4. The time now is 04:17 AM.