可以在awk处理CSV文件,其中包含引用字段中的逗号? [英] Can awk deal with CSV file that contains comma inside a quoted field?

查看:349
本文介绍了可以在awk处理CSV文件,其中包含引用字段中的逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用awk来执行CSV文件数一列的总和。数据格式是一样的东西:

I am using awk to perform counting the sum of one column in the csv file. The data format is something like:

id, name, value
1, foo, 17
2, bar, 76
3, "I am the, question", 99

我就是用这个awk脚本来算的总和:

I was using this awk script to count the sum:

awk -F, '{sum+=$3} END {print sum}'

在一些名称字段值包含逗号,这打破我的awk脚本。
我的问题是:能AWK解决这个问题?如果是的话,我该怎么办呢?

Some of the value in name field contains comma and this break my awk script. My question is: can awk solve this problem? If yes, and how can I do that?

感谢您。

推荐答案

你写一个AWK之类的函数如下:

you write a function in awk like below:

$ awk 'func isnum(x){return(x==x+0)}BEGIN{print isnum("hello"),isnum("-42")}'
0 1

您可以在脚本中包含此功能,并检查第三个字段是数字或not.if不是数字,然后去第四场,如果第四场inturn不是数字小去5 ...直到你达到一个数字value.probably循环将有助于在这里,并把它添加到总和。

you can incorporate in your script this function and check whether the third field is numeric or not.if not numeric then go for the 4th field and if the 4th field inturn is not numberic go for 5th ...till you reach a numeric value.probably a loop will help here, and add it to the sum.

这篇关于可以在awk处理CSV文件,其中包含引用字段中的逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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