The UNIX and Linux Forums  

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
panknil panknil is offline
Registered User
  
 

Join Date: Apr 2007
Location: Kolkata
Posts: 84
Thumbs down How to trim the leading zeroes in a Currency field ?

Dear ghostdog74,

can you please explain the function of
~ and sub(/^\+0+/,"",$4) in your code?

Thanks in advance
Regards,
Pankaj
  #2 (permalink)  
Old 05-29-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,558
Quote:
Originally Posted by panknil
can you please explain the function of
~ and sub(/^\+0+/,"",$4) in your code?
you can try ygor's method.
  #3 (permalink)  
Old 05-29-2007
panknil panknil is offline
Registered User
  
 

Join Date: Apr 2007
Location: Kolkata
Posts: 84
How to trim the leading zeroes in a Currency field ?

Dear Ghostdog74,

i have tried ygor's method its working fine...
but i want to know the function of ~ there...please explain...

Thnks,
Regards,
  #4 (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,411
Actually, there is a precision error in mine. You would need to set CONVFMT to fix...
Code:
$ awk '$4+=0' file
Your bill of 2780.96 for a/c no. 25287324 is due on 11-06.
Your bill of 422270 for a/c no. 28931373 is due on 11-06.
$ awk -v CONVFMT='%.2f' '$4+=0' file
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.
$


Last edited by Ygor; 05-29-2007 at 03:22 AM..
  #5 (permalink)  
Old 05-29-2007
Amruta Pitkar Amruta Pitkar is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 54
Hello ghostdog
Can u please explain ur awk script ?

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

Join Date: Sep 2006
Posts: 2,558
Quote:
Originally Posted by Amruta Pitkar
Hello ghostdog
Can u please explain ur awk script ?

Thanks.
in your sample file, the currency value is in the 4th field.
/^\+0+/ : means match the start with literal + sign and then one or more 0's. (the '+' sign after the 0).
sub(/^\+0+/,"",$4) : means if $4 matched, substitute with "", meaning null..ie it removes them...
  #7 (permalink)  
Old 05-29-2007
Amruta Pitkar Amruta Pitkar is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 54
Hello ghostdog74

I get the follow. error when I try to implement ur awk script.

awk: 0602-521 There is a regular expression error.
*?+ not preceded by valid expression
The source line number is 1.
The error context is
$4 ~ >>> /^+/{ <<<

Pls. guide.
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 09:59 PM.


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