How to set the field separator to an empty string?

2019-01-28 18:08发布

The awk manual indicates that both -v FS and -F are equivalent ways to set the field separator.

The GNU Awk User’s Guide -> 4.5.4 Setting FS from the Command Line:

FS can be set on the command line. You use the `-F' argument to do so.

(...)

The value used for the argument to `-F' is processed in exactly the same way as assignments to the built-in variable FS.

However, I noticed that there is a difference if we set it to an empty string, it is not the same. Tested on my GNU Awk 4.1.1.

This works:

$ awk -F, '{print $2}' <<< "a,b,c"
b
$ awk -v FS=, '{print $2}' <<< "a,b,c"
b

But this does not:

$ awk -F="" '{print $2}' <<< "abc"
                                      # $1 contains abc
$ awk -v FS="" '{print $2}' <<< "abc"
b

Why? Is this because setting FS to empty is a gawk specific?

标签: awk gawk
2条回答
女痞
2楼-- · 2019-01-28 18:14

Looks like you can do this:

$ awk -F '' '{print $2}' <<< "abc"
b

Tested on GNU awk (versions 3.0.4 and 4.1.1) and mawk version 1.2

To be clear, the space between -F and '' is important!

查看更多
Emotional °昔
3楼-- · 2019-01-28 18:38

Why? Is this because setting FS to empty is a gawk specific?

Note that the standards say that the results are unspecified if an empty string is assigned to FS. Some versions of awk will produce the output you showed above in your example. The version of awk on OS/X issues the warning and output.

awk: field separator FS is empty

So the special meaning of setting FS to an empty string, does not work in every awk.

查看更多
登录 后发表回答