How to set the field separator to an empty string?

自作多情 提交于 2019-12-18 08:56:46

问题


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?


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/31135251/how-to-set-the-field-separator-to-an-empty-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!