How to delete trailing 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.

How to delete trailing zeros from a variable

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-23-2007
Chandu2u Chandu2u is offline
Registered User
 
Join Date: May 2007
Last Activity: 25 January 2008, 5:59 AM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
How to delete trailing zeros from a variable

Hi All
I want to delete trailing zeros from varible.
ex:
if variable value is 1234.567000 result as 1234.567
if variable has 1234.0000 result as 1234
if variable as abcd.fgh result as abcd.fgh

Can somone give me a solution using awk?
Sponsored Links
    #2  
Old 05-23-2007
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 31 July 2014, 8:46 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts

Code:
$ echo 1234.567000  | awk ' sub("\\.*0+$","") '
1234.567
$
$ echo 1234.000  | awk ' sub("\\.*0+$","") '
1234

Sponsored Links
    #3  
Old 05-23-2007
Chandu2u Chandu2u is offline
Registered User
 
Join Date: May 2007
Last Activity: 25 January 2008, 5:59 AM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Thx

Thx a lot is thr any way adding check that if it is a float den remove trailing zeros ?
    #4  
Old 05-23-2007
Chandu2u Chandu2u is offline
Registered User
 
Join Date: May 2007
Last Activity: 25 January 2008, 5:59 AM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
i tried with above solution bt if variable has 1234 den it is returning null bt i want as 1234 it self
Sponsored Links
    #5  
Old 05-23-2007
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 31 July 2014, 8:46 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts
Quote:
Originally Posted by Chandu2u
i tried with above solution bt if variable has 1234 den it is returning null bt i want as 1234 it self

Code:
$ echo 1234  | awk ' { sub("\\.*0+$","");print} '
1234

Sponsored Links
    #6  
Old 05-23-2007
Chandu2u Chandu2u is offline
Registered User
 
Join Date: May 2007
Last Activity: 25 January 2008, 5:59 AM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Thx ...its working fine now ... bt i am unable to store the result in variable if i redirecting to file it is working fine ... y is it so??

Result= `echo $value | awk ' { sub("\\.*0+$","");print} '` # Not working

echo $value | awk ' { sub("\\.*0+$","");print} ' > File # working
Sponsored Links
    #7  
Old 05-23-2007
Chandu2u Chandu2u is offline
Registered User
 
Join Date: May 2007
Last Activity: 25 January 2008, 5:59 AM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
it is failing for below value
echo 4000 | awk ' { sub("\\.*0+$","");print} '

getting result as 4
i want it as 4000 ... plz help me on this
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
delete trailing whitespace from end of each line in column 1 only danieladna UNIX for Dummies Questions & Answers 3 01-12-2012 03:03 PM
tr command to delete zeros dean_amrita Shell Programming and Scripting 2 05-23-2011 09:44 AM
How to delete ending/trailing spaces using awk,sed,perl? cola Shell Programming and Scripting 5 08-21-2010 06:52 AM
Removing trailing zeros using sed grajp002 Shell Programming and Scripting 12 03-24-2010 05:13 PM
Delete trailing white space cillmor Shell Programming and Scripting 5 06-19-2009 09:51 AM



All times are GMT -4. The time now is 02:32 PM.