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

查看:999
本文介绍了如何使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

现在,结果我想在最后:

Now, the result I want at the end:

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

我用下面的命令:

I used the following command:

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忽略双引号里面的字段分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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