如何让 awk 忽略双引号内的字段分隔符? [英] How to make awk ignore the field delimiter inside double quotes?

查看:72
本文介绍了如何让 awk 忽略双引号内的字段分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除逗号分隔值文件中的 2 列.考虑 csv 文件中的以下行:

I need to delete 2 columns in a comma seperated values file. Consider the following line in the csv file:

"abc@xyz.com,www.example.com",field2,field3,field4
"def@xyz.com",field2,field3,field4

现在,我想要的结果:

"abc@xyz.com,www.example.com",field4
"def@xyz.com",field4

我使用了以下命令:

awk 'BEGIN{FS=OFS=","}{print $1,$4}'

但是引号内的嵌入逗号造成了问题,以下是我得到的结果:

But the embedded comma which is inside quotes is creating a problem, Following is the result I am getting:

"abc@xyz.com,field3
"def@xyz.com",field4

现在我的问题是如何让 awk 忽略双引号内的,"?

Now my question is how do I make awk ignore the "," which are inside the double quotes?

推荐答案

来自 GNU awk 手册 (http://www.gnu.org/software/gawk/manual/gawk.html#Splitting-By-Content):

From the GNU awk manual (http://www.gnu.org/software/gawk/manual/gawk.html#Splitting-By-Content):

$ awk -vFPAT='([^,]*)|("[^"]+")' -vOFS=, '{print $1,$4}' file
"abc@xyz.com,www.example.com",field4
"def@xyz.com",field4

并查看 使用 awk 高效解析 CSV 的最可靠方法是什么? 更一般地解析包含换行符的 CSV,字段内等.

and see What's the most robust way to efficiently parse CSV using awk? for more generally parsing CSVs that include newlines, etc. within fields.

这篇关于如何让 awk 忽略双引号内的字段分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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