[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Strange behavior in gawk
From: |
Davide Brini |
Subject: |
Re: [bug-gawk] Strange behavior in gawk |
Date: |
Sun, 7 Dec 2014 01:52:04 +0100 |
On Fri, 05 Dec 2014 23:36:10 -0500, "Derrik Walker v2.0"
<address@hidden> wrote:
> This is hard to explain, so I'll just use my code to explain ...
>
> I have a test file, we'll call test.txt:
>
> 1:2:3
> 4:5:6
> .a.b.c
> .e.f.g
>
> I have an awk script, we'll call test.awk:
>
> /:/ {
> FS=":"
> print "NF = " NF " - " $0
> }
>
> /\./ {
> FS="."
> print "NF = " NF " - " $0
> }
>
> If I run the file with gawk, I get:
>
> # gawk -f test.awk test.txt
> NF = 1 - 1:2:3
> NF = 3 - 4:5:6
> NF = 1 - .a.b.c
> NF = 4 - .e.f.g
>
> As you can see, the first entry of each FS is wrong!
This is becoming sort of a FAQ, please read
https://www.gnu.org/software/gawk/manual/gawk.html#Field-Splitting-Summary
the box titled "Changing FS Does Not Affect the Fields". To workaround your
issue you can use split(), or change FS before awk attempts to read the
line in question (ie, at the end of the previous one).
--
D.