Parsing a comma seperated file with shell

Published: Thursday, Sep 24, 2009 Last modified: Thursday, Nov 14, 2024

Make sure you read the file line by line correctly using a while read construct.

while read -r line
do
	IFS=","
	set -- $line
	echo $4 >> $1.mdwn
	echo "$line"
done < "m4.2_testplan.csv"

Use set to parse the CSV. It doesn’t do a perfect job, but it’s usually ok.

In the above example I’m splitting a test description from column 4 into a file named after the id from column 1.