CSV parse with IFS bash : choose “;” as the separator

限于喜欢 提交于 2019-12-24 01:58:37

问题


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.

Here what i did :

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

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

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

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 ; ?


回答1:


You could use 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"

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




回答2:


if you are OK with perl, then:

# cat version 
"Hi";"i";"got;a problem"

# perl -MText::ParseWords -n -l -e 'print $_ for parse_line(";", 1, $_);' version
"Hi"
"i"
"got;a problem"

I am sure there should be a way to achieve the same with awk

I could manage with sed:

# cat version | sed 's/;\("[^"]*"\)*/\n\1/g'
"Hi"
"i"
"got;a problem"


来源:https://stackoverflow.com/questions/20284578/csv-parse-with-ifs-bash-choose-as-the-separator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!