Computing.Net > Forums > Unix > unix awk printf

unix awk printf

Reply to Message Icon

Original Message
Name: nnn22
Date: May 23, 2004 at 18:22:24 Pacific
Subject: unix awk printf
OS: winXP
CPU/Ram: celeron 1GHz/256
Comment:

Hi
how to filter this variable(or data)below into only numbers multiple of 0.02(i.e. 0.00, 0.02, 0.04, ...1.00)
using awk (may be with printf) command?:

input
0.80
0.71
0.72
0.84
0.80
0.81

output
0.80
0.72
0.84
0.80
please help


Report Offensive Message For Removal

Response Number 1
Name: Wolfbone
Date: May 23, 2004 at 19:53:12 Pacific
Subject: unix awk printf
Reply: (edit)

awk '{if ((100*$1 % 2) == 0) print}'



Report Offensive Follow Up For Removal

Response Number 2
Name: nnn22
Date: May 25, 2004 at 16:01:15 Pacific
Subject: unix awk printf
Reply: (edit)

thank you Wolfbone for your help

but I have strange result with some values
in if statement condition(100*$1 % 2)like :
(0.14) should be(0) I get (1.77636e-15)
(0.28) should be(0) I get (3.55271e-15)
(0.56) should be(0) I get (7.10543e-15)
(0.58) should be(0) I get (2)
which stop these values to show in the output, I tryed Bourne shell and Bash shell gives the same result its very strange, is it a bug in awk or something wrong happen somewhere? it works fine if you use (bc) but not awk, try it your self !!!!!


Report Offensive Follow Up For Removal

Response Number 3
Name: Wolfbone
Date: June 1, 2004 at 06:28:30 Pacific
Subject: unix awk printf
Reply: (edit)

Heh! - yes it's not very impressive. Awk just treats everything as a float and uses the crude machine arithmetic to do it's work. I should have realised that would happen and it's not really a bug in awk. I see you already found a workaround but you could also test for the remainder of the last digit on division by 2.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: unix awk printf

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software