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
Loop problem with one more problem aliahsan81 Shell Programming and Scripting 3 01-07-2009 02:02 AM
problem in finding a hardware problem girish.batra SUN Solaris 8 09-09-2008 11:10 AM
ssh script problem problem pcjandyala Shell Programming and Scripting 2 07-31-2008 04:27 PM
Multiplying Floats/Decimals rleebife Shell Programming and Scripting 10 08-02-2007 06:22 AM
SSH Problem auth problem budrito UNIX for Advanced & Expert Users 1 03-17-2004 10:12 AM

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 03-17-2009
JCastro JCastro is offline
Registered User
  
 

Join Date: Mar 2009
Location: Canary Is. Spain
Posts: 5
problem with AWK and floats below 0

Hello. I have a problem with AWK and floats below 0 in a script.
It may be simplified to this line (please, take into account that my "locale" is Spanish, so the system will read "," as decimal separator):


Code:
echo -1,25 2,55745 0,33 ,278 | awk '{print $1+1, $2+1, $3+1, $4+1}'

... getting:

Code:
-0,25 3,55745 1 1

So, what about the third and fourth fields? Why do you think AWK is not reading correctly?

I have tried different options (also with printf options), but I am always at the same point: if the number is 0,xxx AWK will not read the decimal part.
Thank you very much in advance,
  #2 (permalink)  
Old 03-17-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,334
Try:


Code:
awk '{printf("%.2f %.2f %.2f %.2f\n", $1+1, $2+1, $3+1, $4+1)}'

Regards
  #3 (permalink)  
Old 03-17-2009
JCastro JCastro is offline
Registered User
  
 

Join Date: Mar 2009
Location: Canary Is. Spain
Posts: 5
No, It seems not to be working ok. I think that my spanish "locale" configuration may be related with the cause. But maybe any solution involving "format" specification in the inputs?.
As far I know, AWK stores data as 'strings', and makes automatically the numeric conversion if a valid numeric string is found. So Is there any specific instruction to do it manually? I said, for example, conserve the complete string and make the numeric conversion in a second step?
Many Thanks again.
  #4 (permalink)  
Old 03-17-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,334
Try this, first the commas are replaced with dots and after formatting the line (variable s), the dots are replaced with commas:


Code:
awk '{
  gsub(",", ".")
  s=sprintf("%.2f %.2f %.2f %.2f\n", $1+1, $2+1, $3+1, $4+1)
  gsub("\.",",",s);print s}
'

Regards
  #5 (permalink)  
Old 03-17-2009
JCastro JCastro is offline
Registered User
  
 

Join Date: Mar 2009
Location: Canary Is. Spain
Posts: 5
It must be a problem with the quite old "Red Hat" in the pc I was working in. I have tried the same in an Fedora updated machine and AWK works fine again.
So thank you very much Franklin51.
Regards
Closed Thread

Bookmarks

Tags
awk float

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 04:03 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