如何设置字段分隔符为空字符串? [英] How to set the field separator to an empty string?

查看:147
本文介绍了如何设置字段分隔符为空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

awk的手册表明两个 -v FS -F 是等价的方式来设置字段分隔符。

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

的GNUAwk用户指南 - > 4.5 0.4通过命令行设置 FS:

FS可以在命令行上设置。您可以使用`-F参数来这样做。

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

(...)

用于参数`-F的价值完全被处理
  相同的方式分配给内置变量FS

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

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.

本作品:

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

但是,这并不:

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

为什么呢?这是因为设置 FS 清空是一个 GAWK 具体的?

推荐答案

看起来你可以这样做:

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

在测试了GNU AWK(版本3.0.4和4.1.1)和mawk 1.2版

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

需要明确的是,之间的空格 -F 重要

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

这篇关于如何设置字段分隔符为空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆