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
Triml leading zeros in unix kingofprussia UNIX for Dummies Questions & Answers 7 08-07-2008 05:04 AM
Help needed in padding leading zeros jakSun8 Shell Programming and Scripting 12 07-02-2008 07:41 PM
Removing leading zeros from a variable toshidas2000 Shell Programming and Scripting 6 02-27-2008 01:13 PM
Leading zeros wtofu Shell Programming and Scripting 1 09-16-2006 02:52 PM
Conversion to display leading zeros for numerics dsimpg1 Shell Programming and Scripting 4 12-02-2005 11:43 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 11-09-2006
Manish Jha Manish Jha is offline
Registered User
  
 

Join Date: Dec 2005
Location: Boston, USA
Posts: 65
how to retain leading zeros

Hi All,

I am working with a fixed width file Forrmat.
C1 Number (10,3)
C2 Number (10,3)

e.g.
c1= 0000000100.000
c2= 0000000020.000
0000000100.0000000000020.000

I have to perform c1 - c2 . i.e. I want answer to be 0000000080.000. but I am loosing the leading zeros( only getting 80.000) when I am manipulating the file using awk. I tired with printf(%10.3f,c1-c2), but i am unable to retain leading zeros in the output.

Is there any way to retain leading zeros. As of now I could think of writing a logic to get the original length of c1 and then padd that many zeros to the final value of c1-c2. Any body has any other thoughts or any easy way to get this?

Thanks,
--Manish
  #2 (permalink)  
Old 11-09-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,748
try printf(%013.3f,c1-c2)
  #3 (permalink)  
Old 11-09-2006
Manish Jha Manish Jha is offline
Registered User
  
 

Join Date: Dec 2005
Location: Boston, USA
Posts: 65
It works !!

-- Manish
  #4 (permalink)  
Old 11-09-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,121
Code:
echo '0000000100.0000000000020.000' | nawk '{c1=substr($0,1,14); c2=substr($0,15); printf("c1->[%s] c2->[%s] diff->[%014.3f]\n", c1, c2, c1-c2)}'
Closed Thread

Bookmarks

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 05:31 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