Computing.Net > Forums > Unix > unix awk printf

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

unix awk printf

Reply to Message Icon

Name: nnn22
Date: May 23, 2004 at 18:22:24 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: Wolfbone
Date: May 23, 2004 at 19:53:12 Pacific
Reply:

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



0

Response Number 2
Name: nnn22
Date: May 25, 2004 at 16:01:15 Pacific
Reply:

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 !!!!!


0

Response Number 3
Name: Wolfbone
Date: June 1, 2004 at 06:28:30 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: unix awk printf

Printf in AWK www.computing.net/answers/unix/printf-in-awk/4502.html

need help-unix script www.computing.net/answers/unix/need-helpunix-script/6516.html

awk in shell script www.computing.net/answers/unix/awk-in-shell-script/6200.html