如何使用awk提取带引号的字段?
我用来
awk '{ printf "%s", $3 }'
从空格分隔的行中提取一些字段。当然,当字段引用内部有可用空间时,我会得到部分结果。有人可以提出解决方案吗?
I am using
awk '{ printf "%s", $3 }'
to extract some field from a space delimited line. Of course I get partial results when the field is quoted with free spaces inside. May any body suggest a solution please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下次显示您的输入文件和所需的输出。要获取引用字段,
show your input file and desired output next time. To get quoted fields,
这其实是相当困难的。我想出了以下 awk 脚本,该脚本手动分割行并将所有字段存储在数组中。
This is actually quite difficult. I came up with the following
awk
script that splits the line manually and stores all fields in an array.这是解决此问题的可能的替代解决方案。它的工作原理是查找以引号开头或结尾的字段,然后将它们连接在一起。最后它会更新字段和 NF,因此如果您在进行合并的模式之后放置更多模式,则可以使用所有正常的 awk 功能来处理(新)字段。
我认为这仅使用 POSIX awk 的功能并且不依赖于 gawk 扩展,但我不完全确定。
在像这样的输入文件上:
它会生成:
Here's a possible alternative solution to this problem. It works by finding the fields that begin or end with quotes, and then joining those together. At the end it updates the fields and NF, so if you put more patterns after the one that does the merging, you can process the (new) fields using all the normal awk features.
I think this uses only features of POSIX awk and doesn't rely on gawk extensions, but I'm not completely sure.
On an input file like:
it produces:
如果您只是寻找特定领域,那么
就可以。它按“分割文件,因此上例中的第二个字段就是您想要的字段。
If you are just looking for a specific field then
works. It splits the file by ", so the 2nd field in the example above is the one you want.