#!/bin/bash # Some basic CSV processing functions for shell scripts # # Stephen Thomas 12-Jun-2009 # # This is free software. You can do whatever you want with it # except hold me accountable for any grief it causes you. # Regex components q='"' # quote w=$' \t' # whitespace nq="^$q" # not quote nwc="^$w," # not whitespace or comma nwqc="^$w$q," # not whitespace, quote or comma uqf="[$nwqc]([$w]*[$nwc])*" # unquoted field qf="($q[$nq]*$q)*" # quoted field iqf="($q[$nq]*$q)*$q[$nq]*" # incomplete quoted field csv="[$w]*($qf|$uqf)[$w]*,[$w]*(.*)" # comma separated values # Read one CSV record from standard input and make its values available # in the fields[] array. Set corresponding entries in the quoted[] array # to 1 if the value was quoted in the CSV record, 0 if it was bare. function csvread () { unset fields unset quoted nf=0 hold= while IFS= read -r line || return while [[ "$hold$line" =~ ^$csv$ ]] do fields[nf++]="${BASH_REMATCH[1]}" line="${BASH_REMATCH[4]}" hold= done [[ "$hold$line" =~ ^$iqf$ ]] do hold="$hold$line"$'\n' done fields[nf++]="$hold$line" for (( i=0; i