Numeric strings obtained from the input source, will be treated as numeric values, when compared with other strings containing numeric values. Strings valued defined as constants using doublequote enclosures will be treated as strings of characters and compared lexically. The behaviour of the operators when one value is considered to be numeric (eg from the input source), but the other value has been defined explicitly as a numeric string by using doublequote enclosures may also vary depending on which awk interpreter is being used:
BEGIN { a=0 b=0 print "Test 1: 0 and 0" compare(a,b) a="0" b=0 print "Test 2: '0' and 0" compare(a,b) a="0.0" b=0 print "Test 3: '0.0' and 0" compare(a,b) a="0.0" b="0" print "Test 4: '0.0' and '0'" compare(a,b) a="00" b="0" print "Test 5: '00' and '0'" compare(a,b) a="00" b=0 print "Test 6: '00' and 0" compare(a,b) a=-0 b=0 print "Test 7: -0 and 0" compare(a,b) a="-0" b=0 print "Test 8: '-0' and 0" compare(a,b) a="-0" b="0" print "Test 9: '-0' and '0'" compare(a,b) a=100 b=21 print "Test 10: 100 and 21" compare(a,b) a="100" b=21 print "Test 11: '100' and 21" compare(a,b) a=100 b="21" print "Test 12: 100 and '21'" compare(a,b) a="100" b="21" print "Test 13: '100' and '21'" compare(a,b) } function compare(a,b) { if (a == b) { print "Comparison A: == The values are equal" } if (a != b) { print "Comparison B: != The values are not equal" } if (a > b) { print "Comparison C: > The first value comes after the second" } if (a < b) { print "Comparison D: < The first value comes before the second" } if (a >= b) { print "Comparison E: >= The first value does not comes before the second" } if (a <= b) { print "Comparison F: <= The first value does not come after the second" } }
Here is the expected output:
Test 1: 0 and 0 Comparison A: == The values are equal Comparison E: >= The first value does not comes before the second Comparison F: <= The first value does not come after the second Test 2: '0' and 0 Comparison A: == The values are equal Comparison E: >= The first value does not comes before the second Comparison F: <= The first value does not come after the second Test 3: '0.0' and 0 Comparison B: != The values are not equal Comparison C: > The first value comes after the second Comparison E: >= The first value does not comes before the second Test 4: '0.0' and '0' Comparison B: != The values are not equal Comparison C: > The first value comes after the second Comparison E: >= The first value does not comes before the second Test 5: '00' and '0' Comparison B: != The values are not equal Comparison C: > The first value comes after the second Comparison E: >= The first value does not comes before the second Test 6: '00' and 0 Comparison B: != The values are not equal Comparison C: > The first value comes after the second Comparison E: >= The first value does not comes before the second Test 7: -0 and 0 Comparison A: == The values are equal Comparison E: >= The first value does not comes before the second Comparison F: <= The first value does not come after the second Test 8: '-0' and 0 Comparison B: != The values are not equal Comparison D: < The first value comes before the second Comparison F: <= The first value does not come after the second Test 9: '-0' and '0' Comparison B: != The values are not equal Comparison D: < The first value comes before the second Comparison F: <= The first value does not come after the second Test 10: 100 and 21 Comparison B: != The values are not equal Comparison C: > The first value comes after the second Comparison E: >= The first value does not comes before the second Test 11: '100' and 21 Comparison B: != The values are not equal Comparison D: < The first value comes before the second Comparison F: <= The first value does not come after the second Test 12: 100 and '21' Comparison B: != The values are not equal Comparison D: < The first value comes before the second Comparison F: <= The first value does not come after the second Test 13: '100' and '21' Comparison B: != The values are not equal Comparison D: < The first value comes before the second Comparison F: <= The first value does not come after the second