0

Using awk to parse a string on the command line character by character:

echo "foo" | awk 'BEGIN { FS = "" }{ for (n=1; n<=NF; n++ ) print $n }'

works fine. We can also set the value of FS on the command line. However this:

echo "foo" | awk -F '' '{ for (n=1; n<=NF; n++ ) print $n }'

yields the following error message:

awk: field separator FS is empty

Why don't both methods return identical results?

Important: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)

user2243670
  • 345
  • 3
  • 8

1 Answers1

0

Update your version of awk?

$ bash --version | head -n1
GNU bash, version 4.3.42(1)-release (x86_64-pc-linux-gnu)
$ gawk --version | head -n 1
GNU Awk 4.1.1, API: 1.1 (GNU MPFR 3.1.3, GNU MP 6.0.0)
$ mawk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

compiled limits:
max NF             32767
sprintf buffer      2040
$ echo "foo" | gawk -F '' '{ for (n=1; n<=NF; n++ ) print $n }'
f
o
o
$ echo "foo" | mawk -F '' '{ for (n=1; n<=NF; n++ ) print $n }'
f
o
o
$ echo "foo" | gawk 'BEGIN { FS = "" }{ for (n=1; n<=NF; n++ ) print $n }'
f
o
o
$ echo "foo" | mawk 'BEGIN { FS = "" }{ for (n=1; n<=NF; n++ ) print $n }'
f
o
o
Michael Back
  • 1,821
  • 1
  • 16
  • 17