Article View: comp.bugs.4bsd
Article #40echo 1206669121 1206669122 | awk '{if ($1 == $2) print}'
From: Tim Shepard
Date: Tue, 18 Jul 1989 21:34
Date: Tue, 18 Jul 1989 21:34
55 lines
2169 bytes
2169 bytes
I'm using 4.3BSD on a VAX.
echo 1206669121 1206669122 | awk '{if ($1 == $2) print}'
When I run this, it does print the line. It seems that the strings
are being converted to numbers before the comparison. But on page
USD:19-5 of the 4.3 BSD manuals (the ones printed by USENIX), it says,
"Whether a field is deemed numeric or string depends on context; in
ambiguous cases like
if ($1 == $2) ...
fields are treated as strings." The documentation seems to be wrong.
I think the type coercion behaviour of awk is correct because in this
case:
echo 2.0 2 | awk '{if ($1 == $2) print)}'
I do want the test to succeed. Other surprising results can be seen
by running these tests:
echo 1206669121 1206669122 | awk '{print $2-$1}'
echo 1206669119 1206669120 | awk '{print $2-$1}'
I have solved my problems by applying the patch below and building my
private copy of awk (called "dawk") which uses doubles instead of
floats for its internal representation of numbers.
Maybe this patch should be considered for the 4.4 BSD awk?
-Tim Shepard
<shep@ptt.lcs.mit.edu>
*** /usr/src/bin/awk/awk.def Sat Dec 8 16:33:31 1984
--- awk.def Mon Jul 10 11:12:04 1989
***************
*** 1,7 ****
/* awk.def 4.3 83/12/09 */
#define hack int
! #define AWKFLOAT float
#define xfree(a) { if(a!=NULL) { yfree(a); a=NULL;} }
#define strfree(a) { if(a!=NULL && a!=EMPTY) { yfree(a);} a=EMPTY; }
#define yfree free
--- 1,7 ----
/* awk.def 4.3 83/12/09 */
#define hack int
! #define AWKFLOAT double
#define xfree(a) { if(a!=NULL) { yfree(a); a=NULL;} }
#define strfree(a) { if(a!=NULL && a!=EMPTY) { yfree(a);} a=EMPTY; }
#define yfree free
Technical Details
Message-ID:
<SHEP.89Jul18173438@allspice.lcs.mit.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!think!mintaka!daemonShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!think!mintaka!daemon
From: shep@allspice.lcs.mit.edu (Tim Shepard)
Newsgroups: comp.bugs.4bsd
Subject: echo 1206669121 1206669122 | awk '{if ($1 == $2) print}'
Message-ID: <SHEP.89Jul18173438@allspice.lcs.mit.edu>
Date: 18 Jul 89 21:34:40 GMT
Sender: daemon@mintaka.lcs.mit.edu (Lucifer Maleficius)
Distribution: comp
Organization: MIT Laboratory for Computer Science
Lines: 55