bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Assigning to NF


From: Aharon Robbins
Subject: Re: Assigning to NF
Date: Sat, 21 Mar 2009 22:00:41 +0200

Thanks for the note.  To add more fields, POSIX says just increase NF
to the desired value.  The record is then rebuilt automatically the
next time it's referenced; the $1 = $1 isn't necessary.

The Bell Labs awk doesn't implement these semantics.  I doubt that this
will ever be fixed, but who knows?

Basically, when NF = 0, assigning to $1 doesn't change anything, which
explains gawk's behavior. Whether it's "right" or not, I don't know.
I don't think I want to mess with it though.

Thanks,

Arnold

> Date: Thu, 19 Mar 2009 20:48:50 +0100
> From: Dave B <address@hidden>
> To: address@hidden
> Subject: Assigning to NF
>
> 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.
>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]