Search code examples
bashparsingcsvifs

CSV parse with IFS bash : choose ";" as the separator


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


Solution

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