![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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, |
|
||||
|
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. |
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| awk float |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|