onetrueawk/awk

Unexpected output for $0 in CSV mode

Closed this issue · 1 comments

I noticed that in CSV mode, if one prints the content of $0, the output is CSV-formatted if the row is unchanged, but in standard AWK style otherwise. This code, for instance:

$ awk --csv 'NR == 2 {$2 = "0"} {print $0}' <<< "a,b,c
d,e,f
g,h,i"

produces the output:

a,b,c
d 0 f
g,h,i

Is this intended behavior? It seems more consistent to me to either eliminate the CSV structure entirely after parsing the input or to recreate it within $0 after every field modification.

BTW, really appreciate the project and new features. Thanks.

This behaviour is consistent with how awk has always worked. Whenever you modify a field, the record is rebuilt by separating the fields with the value of OFS. Setting OFS = "," either in a BEGIN rule or via the -v command line option will produce something like what you're expecting, for simple CSV input.