How to parse a CSV file in Bash?
You need to use IFS
instead of -d
:
while IFS=, read -r col1 col2do 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 can't handle by itself. Examples of such tools are cvstool
and csvkit
.
We can parse csv files with quoted strings and delimited by say | with following code
while read -r linedo 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.