linux – How to parse a CSV file in Bash?

linux – How to parse a CSV file in Bash?

You need to use IFS instead of -d:

while IFS=, read -r col1 col2
    echo I got:$col1|$col2
done < myfile.csv

Note that for general purpose CSV parsing you should use a specialized tool which can handle quoted fields with internal commas, among other issues that Bash cant handle by itself. Examples of such tools are cvstool and csvkit.

From the man page:

-d delim
The first character of delim is used to terminate the input line,
rather than newline.

You are using -d, which will terminate the input line on the comma. It will not read the rest of the line. Thats why $y is empty.

linux – How to parse a CSV file in Bash?

We can parse csv files with quoted strings and delimited by say | with following code

while read -r line
    field1=$(echo $line | awk -F| {printf %s, $1} | tr -d )
    field2=$(echo $line | awk -F| {printf %s, $2} | tr -d )

    echo $field1 $field2
done < $csvFile

awk parses the string fields to variables and tr removes the quote.

Slightly slower as awk is executed for each field.

Leave a Reply

Your email address will not be published.