IFS not parsing well CSV

I am trying to parse a file so I can obtain the first column. The command I'm using is:

while IFS=',' read -r a; do echo "$a"; done < test.csv

However it is still outputting the whole csv instead of the first column. An example of the csv is as follows:

    MOIANÃS,42,24/08/2020,Home,Majors de 74,No,0,2,0,0,0,0,0
    ALT CAMP,01,30/07/2020,Dona,Entre 15 i 64,Si,0,0,0,0,0,0,0
    ALT CAMP,01,30/07/2020,Dona,Entre 65 i 74,No,0,1,0,0,0,0,0
    ALT CAMP,01,30/07/2020,Dona,Entre 65 i 74,Si,0,0,0,0,0,0,0

I've been looking elsewhere and all seem to agree that this should be the correct approach when parsing csv using IFS. A thing I've noticed is that if I add a new column to the read function, say b, it outputs the first column instead of everything.

while IFS=',' read -r a b; do echo "$a"; done < test.csv

I don't understand this behaviour and it does not seem to work further than printing the first column. For example, If I were to put c and $c, it wouldn't print the third column and so on.

Can you please explain this behaviour and why this is happening?

Thank you

Read more here: https://stackoverflow.com/questions/64414795/ifs-not-parsing-well-csv

Content Attribution

This content was originally published by js352 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: