awk – 如何将字段分隔符设置为空字符串?

前端之家收集整理的这篇文章主要介绍了awk – 如何将字段分隔符设置为空字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
awk手册表明-v FS和-F都是设置字段分隔符的等效方法.

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.

但是,我注意到如果我们将它设置为空字符串会有所不同,它不一样.在我的GNU Awk 4.1.1上测试过.

这有效:

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

但这不是:

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

为什么?这是因为将FS设置为空是特定的gawk吗?

解决方法

看起来你可以这样做: @H_301_17@$awk -F '' '{print $2}' <<< "abc" b

在GNU awk(版本3.0.4和4.1.1)和mawk版本1.2上测试

要清楚,-F和”之间的空间很重要!

猜你在找的Linux相关文章