The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
trim leading zero in certain column in a string dngo UNIX for Dummies Questions & Answers 2 04-01-2007 03:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-29-2007
Amruta Pitkar Amruta Pitkar is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 54
How to trim the leading zeroes in a Currency field ?

How do I trim the leading zeroes, and (+,-) in the currency field ?

I have a text file.
Your bill of +00002780.96 for a/c no. 25287324 is due on 11-06.
Your bill of +00422270.48 for a/c no. 28931373 is due on 11-06.

I want the O/P file to be like.
Your bill of 2780.96 for a/c no. 25287324 is due on 11-06.
Your bill of 422270.48 for a/c no. 28931373 is due on 11-06.

One of the possible ways I cud think was to write a small function to read char by char and remove the leading zeroes. Is there any other quicker and effcient way ?

Any suggestions ?

Thanks.
  #2 (permalink)  
Old 05-29-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,538
one way
Code:
awk '$4 ~ /^+/{ sub(/^\+0+/,"",$4) }{print $0}' "file"
output:
Code:
# ./test1.sh
Your bill of 2780.96 for a/c no. 25287324 is due on 11-06.
Your bill of 422270.48 for a/c no. 28931373 is due on 11-06.
  #3 (permalink)  
Old 05-29-2007
suparnbector suparnbector is offline
Registered User
  
 

Join Date: May 2007
Posts: 13
try this ....

x=+00002780.96
num=$(( ${x} ))
echo $num
  #4 (permalink)  
Old 05-29-2007
praveenkumar_l's Avatar
praveenkumar_l praveenkumar_l is offline
Registered User
  
 

Join Date: May 2007
Posts: 36
Try this too

sed '/[+-]/,/[1-9]/ s/[+-]00*//g' file
  #5 (permalink)  
Old 05-29-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Quote:
Originally Posted by praveenkumar_l
Try this too

sed '/[+-]/,/[1-9]/ s/[+-]00*//g' file
This sed statement would modify the date too since the '-' symbol is present in the date representation
  #6 (permalink)  
Old 05-29-2007
praveenkumar_l's Avatar
praveenkumar_l praveenkumar_l is offline
Registered User
  
 

Join Date: May 2007
Posts: 36
Quote:
Originally Posted by lorcan
This sed statement would modify the date too since the '-' symbol is present in the date representation
I have corrected my sed command. Try this. It is working fine.

sed 's/[+-]00*\(.*\..*\)/\1/g' file
  #7 (permalink)  
Old 05-29-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Try...
Code:
awk '$4+=0' file
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0