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

2019-07-23 17:45发布

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

2条回答
Rolldiameter
2楼-- · 2019-07-23 18:21

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"
查看更多
\"骚年 ilove
3楼-- · 2019-07-23 18:39

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.)

查看更多
登录 后发表回答