如何忽略bash脚本读取的CSV文件中的逗号 [英] How to ignore commas within a CSV file being read by bash script

查看:56
本文介绍了如何忽略bash脚本读取的CSV文件中的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些字段可以使用逗号作为分隔符来分隔字段,但是某些值实际上包含逗号,例如"California,CA".这些值​​用引号引起来,以指示其中的字符应视为一部分字段,但我不知道如何解析该字段以使其具有相同的值.如何忽略字符串

I have some fields that can separate the fields using the comma as delimiter, but some values actually contain commas, such as ""California , CA"" These values are surrounded by quotes to indicate the characters within should be treated as part of the field, but I don't know how to parse it to take this into same values.How to Ignore the comma(,) between string

推荐答案

如果您的 awk 支持,则使用 FPAT ,它是一个 awk 内置的-in变量,用于定义字段的正则表达式,而不是定界符.IT就像是 FS 的补充,它也是 awk 的内置变量.

Use FPAT if your awk supports, its an awks built-in variable to define the regex for fields rather than the delimiters. ITs like a complement to FS which is also awks builtin variable.

示例:

echo 'hey there,ola,"Nice, command",ola' |awk -v FPAT='[^,]+|"[^"]+"' '{print $1}'
hey there
echo 'hey there,ola,"Nice, command",ola' |awk -v FPAT='[^,]+|"[^"]+"' '{print $2}'
ola
echo 'hey there,ola,"Nice, command",ola' |awk -v FPAT='[^,]+|"[^"]+"' '{print $3}'
"Nice, command"
echo 'hey there,ola,"Nice, command",ola' |awk -v FPAT='[^,]+|"[^"]+"' '{print $4}'
ola

这篇关于如何忽略bash脚本读取的CSV文件中的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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