problem with AWK and floats below 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with AWK and floats below 0
# 1  
Old 03-17-2009
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  
Old 03-17-2009
Try:

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

Regards
# 3  
Old 03-17-2009
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  
Old 03-17-2009
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  
Old 03-17-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk-if problem

Hi This's my problem ( AIX ) switchshow | awk ' { if ( $1==0 ) print $0; }'It works But these below doesn't switchshow | awk ' { if ( $1==0 1 2 3 4 7 9 12 15 ) print $0; }'switchshow | awk ' { if ( $1== 7 9 12 15 ) print $0; }'Somebody help, please :(:(:( (3 Replies)
Discussion started by: bobochacha29
3 Replies

2. Programming

Extracting floats from a string in Python

I have this script which can calculate the total of numbers given in a string total = 0 for c in '0123456789': total += int(c) print total How should I modify this script to find the total of if the numbers given in the string form have decimal places? That is, how do I need to modify... (2 Replies)
Discussion started by: faizlo
2 Replies

3. Shell Programming and Scripting

awk problem - combining awk statements

i have a datafile that has several lines that look like this: 2,dataflow,Sun Mar 17 16:50:01 2013,1363539001,2990,excelsheet,660,mortar,660,4 using the following command: awk -F, '{$3=strftime("%a %b %d %T %Y,%s",$3)}1' OFS=, $DATAFILE | egrep -v "\-OLDISSUES," | ${AWK} "/${MONTH} ${DAY}... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

Calculation with floats

I want to make computations using floats. This cannot be done with csh. ksh seem to have a problem as well. What is good shell for computations without having to resort to bc or awk? How about python? (5 Replies)
Discussion started by: kristinu
5 Replies

5. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

6. UNIX for Dummies Questions & Answers

awk problem

Hi, I obtain a error to execute this code: nawk -F¤ -v campotipo=${CAMPO_TIPO_SEDRA} ficherolog=${DIRECTORIO_LOGS_MARINE_SEDRA}/${F_LOG_SEDRA} -v ficheroerroneos=${LOG_SEDRA_ERRONEOS} -v separador=${SEPARADOR} -v num_campos=${NUMERO_CAMPOS} -v fichero_procesando=${FICHERO_ENTRADA} ' BEGIN { OFS... (2 Replies)
Discussion started by: danietepa
2 Replies

7. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

8. Shell Programming and Scripting

awk problem

I want to assign the file name under my dir and store into variable fileName here is my for loop but awk seems doesn't work numberFile=`ls -l $dir | wc -l` for i in $numberFiles do fileName=` ls -l | awk `{print $8}`` blablabla done thanks for helping (4 Replies)
Discussion started by: cryogen
4 Replies

9. Shell Programming and Scripting

integers, floats and text

I am using gawk in a dos shell in windows xp and want to read a datafile and reformat it. The datafile consists of columns of integers, floating point numbers and text strings. Each column is a fixed width and each column contains the same data type, eg all integers, all text. I am looking for a... (0 Replies)
Discussion started by: lookingfor help
0 Replies

10. Shell Programming and Scripting

Multiplying Floats/Decimals

Is there a way that i can get something like this to work: Number=`expr 80 \* 10.69` i.e. To multiply an integer by a decimal or a decimal by a decimal etc...? thanks (10 Replies)
Discussion started by: rleebife
10 Replies
Login or Register to Ask a Question