Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Removing leading zeros from a variable

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 02-27-2008
DukeNuke2's Unix or Linux Image
DukeNuke2 DukeNuke2 is online now Forum Staff  
Soulman
 
Join Date: Jul 2006
Last Activity: 28 May 2015, 1:58 AM EDT
Location: Berlin, Germany
Posts: 5,492
Thanks: 61
Thanked 245 Times in 238 Posts
have a look at the command "tr"...

hth,
DN2
Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 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 Unix and Linux 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 Unix and Linux 02-27-2008
jaduks's Unix or Linux Image
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 Unix and Linux 02-27-2008
Tytalus's Unix or Linux Image
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 19 May 2015, 6:39 AM EDT
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 02:02 AM.