3 ############################################################################
5 declare -a csv_split=()
7 # you can override the chosen separator if your data has tildes in it...
8 if [ -z "$UNIQUE_SEPARATOR" ]; then
12 # parses a line of CSV text and turns it into an array called "csv_split".
13 # one defect of this approach is that if there are occurrences of the separator
14 # character in the middle of the quoted strings, they will not be handled
16 function parse_csv_line()
18 local parm="$1"; shift
19 #echo line before is: $parm
21 # fix the line so we don't mistake embedded commas as separators.
22 to_split="$(echo "$parm" | sed -e "s/\" *, *\"/\"$UNIQUE_SEPARATOR\"/g")"
23 #echo line afterwards is: $to_split
24 # swap the IFS so we can find the breaks.
26 IFS="$UNIQUE_SEPARATOR"
27 local csv_temp=($to_split)
29 # loop through and strip out the quotes.
31 while [ $i -lt ${#csv_temp[*]} ]; do
32 csv_split[$i]="$(echo ${csv_temp[$i]} | sed -e 's/"//g')"
37 ############################################################################
43 while read input_text; do
44 # echo input_text is $input_text
46 parse_csv_line "$input_text"
48 if [ ${#csv_split[*]} -lt 1 ]; then
49 echo did not see any fields in the split array.
51 echo line splits into ${#csv_split[*]} fields:
53 while [ $i -lt ${#csv_split[*]} ]; do
54 quoteless="${csv_split[$i]}"