Problem with sub command (awk) and numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with sub command (awk) and numbers
# 1  
Old 07-07-2010
Question Problem with sub command (awk) and numbers

Hi,

I am trying to perform a simple soustraction between two floating numbers and cannot get it done for some reason due to the use of the sub command.

The following is the straight-forward result of the soustraction:

Code:
[cscyabl@kna-bassdev scripts]$ echo | gawk '{a=968;b=967.99;c=a-b;print c}'                      
0,01

Now we are working with a different locale where the decimals delimiter is not a "." but a ",". You can see that on the above output and the way it's displayed.

Therefore I want this in:
Code:
sub(/,/,".", a)

And it does not work for some strange reason:
Code:
[cscyabl@kna-bassdev scripts]$ echo | gawk '{a="968";b="967,99";sub(/,/,".",a);sub(/,/,".",b);print a" "b;c=a-b;print c}'
968 967.99
1

The diff is now 1 and I can't explain why...
Any help would be much appreciated.
# 2  
Old 07-07-2010
"sub" should be done after performing mathematical operation:
Code:
echo | gawk '{a="968";b="967,99";c=a-b;sub(/,/,".",a);sub(/,/,".",b);sub(/,/,".",c);print a" "b;print c}'

# 3  
Old 07-07-2010
Code:
gawk 'BEGIN{a=968;b=967.99;c=a-b;printf "%.2f",c}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to evaluate a string of numbers in the same command of AWK

Hi, I am trying to do evaluate one numerical string after substitution. ++++++++++++++++== What I have = "7.04+2.3Xlog(0.72e-6X1.0e6)X1.9596" What I need = evaluate 7.04+2.3*log(0.72e-6*1.0e6)*1.9596 = 5.55941 what I am doing; echo "7.04+2.3Xlog(0.72e-6X1.0e6)X1.9596" | awk... (2 Replies)
Discussion started by: vivek_shm74
2 Replies

2. Shell Programming and Scripting

getting problem in awk command

Hi, I have one file with tab delimited values in it. i want to increase the value of 6th field by 2 if value of 3rd field is greater than 2 . The command is working fine but space between the field is getting removed after adding. below is the file and the command Filename: test1.txt ... (13 Replies)
Discussion started by: ravi_agarwalla
13 Replies

3. Shell Programming and Scripting

A mistake in awk command I used to parse numbers

Hi I have a big file with a certain pattern (shown below) from which I need to parse out some digits in tabular format. The format of the file is: '-' indicates text which doesn't to be parsed # Output of huzzle for sequence file 1000.Clade1.html - - - -- -------... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

4. Shell Programming and Scripting

problem with awk command

I am having problem running an awk commad on a file Before applying awk command on the file After applying I don't expect more than 409743 records in the file. Why do I have 1 record more after applying awk command? Please let me know (4 Replies)
Discussion started by: dsravan
4 Replies

5. Shell Programming and Scripting

Problem in awk command

Hello, I am getting problem in awk command during matching (using if in awk) when there is special character "" I have tried by loosing the special meaning, still its not working Below is my code: set pinname_watch = "DCORRECT" set pinname = "DCORRECT\" echo 'defineGateSize... (2 Replies)
Discussion started by: nehashine
2 Replies

6. Post Here to Contact Site Administrators and Moderators

AWK command problem

How can I write an AWK so it could print some fixed strings and concatenate that with other words from an input file. Also having a string like "go" in the next line in the output file as below: Example: A fixed string like: "Hello my" and the input file is: friend mother father... (4 Replies)
Discussion started by: sybase08
4 Replies

7. Shell Programming and Scripting

awk/sed Command: To Parse Stament between 2 numbers

Hi, I need an awk command that would parse the below expression Input Format 1 'Stmt1 ............................'2 'Stmt2 ............................'3 'Stmt3 ............................'4 'Stmt4 ............................'5 'Stmt5 ............................'6 'Stmt6... (1 Reply)
Discussion started by: rajan_san
1 Replies

8. Shell Programming and Scripting

problem in awk command

Hello all, i am new one to this forum. : i have file with these contents.. internal://project/squid-internal-static/icons/anthony-xpm.gif http://widget.blogrush.com/img/br.png http://www.wingware.com/css/print http://publib.boulder.ibm.com/infocenter/systems/advanced/filterwarning.css... (3 Replies)
Discussion started by: viveksnv
3 Replies

9. Shell Programming and Scripting

grep or awk problem, unable to extract numbers

Hi, I've trouble getting some numbers from a html-file. The thing is that I have several html-logs that contains lines like this: nerdnerd, how_old_r_u:45782<br>APPLY: <hour_second> Verification succeded This is some of what I've extracted from a html file but all I really want is the number... (7 Replies)
Discussion started by: baghera
7 Replies

10. Shell Programming and Scripting

problem with floating point numbers in awk

hi all, i have the following problem using awk in a script i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this num = $4 i cant find a way to convert the variable from string to floating point to perform... (7 Replies)
Discussion started by: kanagias
7 Replies
Login or Register to Ask a Question