Tuesday, June 23, 2015

Parsing csv files in shell script

There are many ways to parse csv file in shell script. Here is the one that works well for me:

Assuming I have this csv file, mycsv.csv:

super,genius
cool,awesome
...

This script, parse_csv.sh, will parse mycsv.csv:

$ nano parse_csv.sh

#! /bin/bash

IFS=','
while read f1 f2
do
       # remove the carriage return character at the end of each line (path column)
       newvar=$(echo $f2 | sed -e 's/\r//g')
       echo "First column: $f1"
       echo "Second column: $newvar"
done < $1


$ sudo chmod +x parse_csv.sh
$ ./parse_csv.sh mycsv.csv