is there a bug in AWK on 6.x and 7.x (fixed in 8.x)?

Dominic Fandrey kamikaze at bsdforen.de
Sun Oct 3 06:18:14 UTC 2010


On 02/10/2010 21:58, Miroslav Lachman wrote:
> I think there is a bug in AWK in base of FreeBSD 6.x and 7.x (tested on
> 6.4 i386 and 7.3 i386)
> 
> I have this simple test case, where I want 2 columns from GeoIP CSV file:
> 
> awk 'FS="," { print $1"-"$2 }' GeoIPCountryWhois.csv

You know that with this syntax FS="," is treated as a condition to
run the following block in angle brackets?

I.e. you assign FS for every line of input. What seems to have
changed is when and how a variable assignment returns true or false
in a boolean condition.

Any way, I doubt it was your intention to conditionally execute the
code, so this is a bug in your programming.

The correct approach to do what you want to is the BEGIN block
if you want to do it in the code.

For your use of head, consider the following:
> awk 'BEGIN {FS=","} NR <= 5 { print $1"-"$2 }' GeoIPCountryWhois.csv

That way the code is executed under the condition that the line
number is less or equal 5.

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? 


More information about the freebsd-stable mailing list