CSV解析与IFS的bash:选择英寸;"作为隔板 [英] CSV parse with IFS bash : choose ";" as the separator

查看:146
本文介绍了CSV解析与IFS的bash:选择英寸;"作为隔板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有130 COLS一个CSV,我需要做3 CSV这一点。
我用了一段时间,IFS循环,因为我需要做些事情上每一行的增值经销商。

I have a CSV with 130 cols and i need to do 3 csv with that. I'm looping with a while and IFS because i need to do someting with the vars on each row.

下面我做了什么:

while IFS=";" read [my 130 vars]
[what i do with the vars] 
done < file.csv

但我有一些行的问题,因为原来的CSV我收到就像是:

But i have a problem on some rows because the original csv i receive is like :

"Hi";"i";"got;a problem"

正如你可以看到我有一个问题,一个;在一个值。以及IFS读它作为两个值的分离。
因此,这里是我的问题:有没有办法采取;作为隔板,而不是仅仅; ?

As you can see i have a problem with a ; in a value. And the IFS read it as the separation of two values. So here is my question : is there a way to take ";" as the separator instead of just ; ?

推荐答案

您可以使用 AWK

gawk 'BEGIN{FPAT="([^;]+)|(\"[^\"]+\")"}{for(i=1;i<=NF;i++){printf ("%s\n",$i)}}' file.csv

有关您的输入,它会产生:

For your input, it'd produce:

"Hi"
"i"
"got;a problem"

(我怀疑它是否可以使用庆典,通过操纵 IFS 即达到预期的效果。)

(I doubt if it's possible to achieve the desired result using bash, i.e. by manipulating IFS.)

这篇关于CSV解析与IFS的bash:选择英寸;&QUOT;作为隔板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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