onetrueawk/awk

Unexpected behavior with var=value where var=FS

ghshephard opened this issue · 4 comments

I think I just found a strange behavior in awk in which setting FS="[ ,]" doesn't work until at least one line in a file is read:

$ cat > t1
1,2 3
4,5 6

shephard@Gordons-Air:~
$ awk '{printf "x"FS"x  "; print $1":"$2}' t1 FS="[ ,]" t1 t1
x x  1,2:3
x x  4,5:6
x[ ,]x  1,2:3
x[ ,]x  4:5
x[ ,]x  1:2
x[ ,]x  4:5
$ awk --version
awk version 20220912

On Linux, with gawk, it does what I would expect:
terapin linux
 awk '{printf "x"FS"x  "; print $1":"$2}' t1 FS="[ ,]" t1 t1
x x  1,2:3
x x  4,5:6
x[ ,]x  1:2
x[ ,]x  4:5
x[ ,]x  1:2
x[ ,]x  4:5

root@terapin2:~# awk --version
GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)

This does work as expected with original awk.
shephard@Gordons-Air:~
$ awk '{printf "x"FS"x  "; print $1":"$2; if (FNR==2) FS="[ ,]"}' t1  t1 t1
x x  1,2:3
x x  4,5:6
x[ ,]x  1:2
x[ ,]x  4:5
x[ ,]x  1:2
x[ ,]x  4:5

It’s more than just opening the file:
shephard@Gordons-Air:~
$ touch empty
$ awk '{printf "x"FS"x  "; print $1":"$2}' t1 FS="[ ,]" empty t1
x x  1,2:3
x x  4,5:6
x[ ,]x  1,2:3
x[ ,]x  4:5

But running through a single line, kicks it in:
shephard@Gordons-Air:~
$ echo -n " " > one
$ awk '{printf "x"FS"x  "; print $1":"$2}' t1 FS="[ ,]" one t1
x x  1,2:3
x x  4,5:6
x[ ,]x  :
x[ ,]x  1:2
x[ ,]x  4:5

I don't think this is expected behavior.

Correct Behavior with PR:

$ ./awk '{printf "x"FS"x  "; print $1":"$2}' t1 FS="[ ,]" t1  t1
x x  1,2:3
x x  4,5:6
x[ ,]x  1:2
x[ ,]x  4:5
x[ ,]x  1:2
x[ ,]x  4:5

And:

$ ./awk '{printf "x"FS"x  "; print $1":"$2}' FS="[ ,]" t1  t1
x[ ,]x  1:2
x[ ,]x  4:5
x[ ,]x  1:2
x[ ,]x  4:5
plan9 commented

thank you for spotting this unusual corner case.

plan9 commented

latest release includes @mpinjr fix.