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
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?:
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.
Summary: I fully agree with you, James on this issue, that one should spilt printfs and work. I ahve done the same thing above and find out the solution to my problem, but the code which i was trying to modify...
Summary: Regarding printf (formatted print), this is also common to unix. You can execute printf commands from the unix prompt (man printf). In the awk printf below, the format mask (in double-quotes), specif...
Summary: Hello Wolfbone, thanks for your answer, I think, I was a little bit blind today. It's so easy with cut and I thought, cut is only for tables ... And that I can't take every UNIX-command in awk - must ...