When specifying a field separator with {FS=","}, the separator is not applied to the first line

Bug #1329736 reported by IanBall
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
mawk (Ubuntu)
Won't Fix
Undecided
Unassigned

Bug Description

I have a csv input file where I want to extract specific fields. When I cat that file into mawk using the {FS=","} within the brackets, the first line of output is as if the field separator was still a space instead of the comma that I specified. From the second line onwards, the comma field separator is honoured.
The first three lines of output from the command I am using are:
$ cat LUN.csv | awk '{FS=","} {print $2","$5","$14","$17","$20","$23","$26","$29","$32","$35","$77","$78","$79","$80","$81","$82","$83","$84","$85","$86","$87","$88","$89","$90","$91","$92","$93","$94","$95","$96","$97","$98}'
60,5;,naa.600601605d212700a1c3b23a8a6ae011;,naa.600601605d212700a1c3b23a8a6ae011;,naa.600601605d212700a1c3b23a8a6ae011],06/02/2014,,,,,,,,,,,,,,,,,,,,,,,,,,,
06/02/2014 12:05:22,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
06/02/2014 12:07:36,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

If I try the command using the -F "," flag instead the first line of output also has the comma field separator honoured.
The first three lines of output from the command I am using are:
$ cat LUN.csv | awk -F "," '{print $2","$5","$14","$17","$20","$23","$26","$29","$32","$35","$77","$78","$79","$80","$81","$82","$83","$84","$85","$86","$87","$88","$89","$90","$91","$92","$93","$94","$95","$96","$97","$98}'
06/02/2014 12:02:59,0.158562,0.106358,3.061710,0.102991,38.801684,2.717995,0.003367,10.031008,0.343715,0,0,0,0,218,10,16,270,0,0,0,0,0,0,47,12,0,2,4,0,0,0
06/02/2014 12:05:22,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
06/02/2014 12:07:36,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

I would expect that the behaviour of both would be the same, and specifically that of the {FS=","} would be applied to the first line as well as all following lines.

The attached file is the head of the file I am using so you can test.

The version I am using is on kubuntu 13.10, the version information output is:
$ mawk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

compiled limits:
max NF 32767
sprintf buffer 2040

Please note that although I have awk in the example command lines above, it is pointing to mawk.
$ awk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

compiled limits:
max NF 32767
sprintf buffer 2040
$ readlink -f /usr/bin/awk
/usr/bin/mawk
$

Revision history for this message
IanBall (ian-onlineloop) wrote :
IanBall (ian-onlineloop)
summary: When specifying a field separator with {FS=","}, the separator is not
- applied tp the first line
+ applied to the first line
description: updated
description: updated
Revision history for this message
Greg Lutostanski (lutostag) wrote :

Thanks for reporting this bug and taking the time to help make Ubuntu better!

Unfortunately what you are experiencing is a quirk of how awk works [of which there are many :)]. Field separators are only used for the next line after they are set. To get around this you can use the BEGIN pattern to do this before the first line is read into the awk interpreter -- example given below.

awk 'BEGIN {FS=","} {print $2","$5","$14","$17","$20","$23","$26","$29","$32","$35","$77","$78","$79","$80","$81","$82","$83","$84","$85","$86","$87","$88","$89","$90","$91","$92","$93","$94","$95","$96","$97","$98}'

I tested against mawk as well as busybox's awk which both do the same thing. While this is nonintuitive, it is expected behavior.

I am marking this bug as "won't fix", please comment if you believe I am mistaken.

Thanks again!

Changed in mawk (Ubuntu):
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.