[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Assigning to NF
From: |
Dave B |
Subject: |
Assigning to NF |
Date: |
Thu, 19 Mar 2009 20:48:50 +0100 |
User-agent: |
Thunderbird 2.0.0.19 (X11/20090114) |
What I'd like to do is have a quick and dirty method to increase the number
of fields in a line (adding empty fields, to force all lines to have the
same number of fields). So I thought that modifying NF, with a certain OFS,
and forcing awk to rebuild $0 would do the trick.
Here's what I did at first (gawk 3.1.6):
$ printf 'a,b,c\n' | gawk -v FS=',' -v OFS=',' '{NF=10;$1=$1;print}'
a,b,c,,,,,,,
so far so good. But when the input has an empty line, it does not work:
$ printf 'a,b,c\n\nd,e\n' | gawk -v FS=',' -v OFS=',' '{NF=10;$1=$1;print}'
a,b,c,,,,,,,
d,e,,,,,,,,
Interestingly, if I remove the $1=$1 assignment, it works:
$ printf 'a,b,c\n\nd,e\n' | gawk -v FS=',' -v OFS=',' '{NF=10;print}'
a,b,c,,,,,,,
,,,,,,,,,
d,e,,,,,,,,
Other awk implementations behave differently; mawk and busybox awk work fine
with any input whether the $1=$1 is present or not. Bell labs awk segfaults
if you increment NF by more than 1 and then do $1=$1 but if you omit the
$1=$1, it does not segfault (but does not give the expected result in any case).
I know this is probably "undefined behavior" land (or at least, I did not
find anything in the standard about assigning to NF), so I'm not sure the
inconsistent gawk behavior described above can be reported as a "bug", but
here it is in case it helps.
--
D.
- Assigning to NF,
Dave B <=