From: Chris F.A. Johnson Date: Mon, 4 Apr 2011 19:26:19 +0000 (+0200) Subject: changed CSV parser X-Git-Url: http://git.rot13.org/?p=ink-generator.git;a=commitdiff_plain;h=e6ac56448c0818a1cdee839585da116b6c2c60f0 changed CSV parser --- diff --git a/generator.sh b/generator.sh index d639780..1595f13 100755 --- a/generator.sh +++ b/generator.sh @@ -3,6 +3,10 @@ # Generator - a Inkscape extension to generate end-use files from a model # Copyright (C) 2008 Aurélio A. Heckert # +# Added CSV parser from +# http://fixunix.com/unix/83523-how-do-i-read-split-derive-csv-using-bash-script.html +# by Chris F.A. Johnson http://cfaj.freeshell.org +# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or @@ -48,15 +52,47 @@ Please, give the right name, or give the full path for the CSV file." exit 1 fi -function sep_line { - sed 's/^\([^"]*\("[^"]*"[^"]*\)*"[^"]*\),/\1#COMMAHACK#/g' | - sed 's/,/\n/g' | sed "s/\"\"/\"/g; s/^['\"]\|['\"]$//g" +#function sep_line { +# sed 's/^\([^"]*\("[^"]*"[^"]*\)*"[^"]*\),/\1#COMMAHACK#/g' | +# sed 's/,/\n/g' | sed "s/\"\"/\"/g; s/^['\"]\|['\"]$//g" +#} + +function csv_split() { ## USAGE: csv_split CSV_RECORD + local record=${1%"${CR}"} + local right + local vnum=0 + unset values + while [ -n "$record" ] + do + + case $record in + \"*) right=${record#*\",} + value=${record%%\",*} + values[$vnum]=${value#\"} + ;; + *) values[$vnum]=${record%%,*} + right=${record#*,} + ;; + esac + + case $record in + *,*) record=${right} ;; + *) record=${record#\"} + values[$vnum]=${record%\"} + break;; + esac + + echo ${values[$vnum]} + + vnum=$(( $vnum + 1 )) + + done } # Set column names: eval "$( col=0 - head --lines=1 "$data_file" | sep_line | + csv_split `head --lines=1 "$data_file"` | while read name; do let col++ if [ "$var_type" == "name" ]; then @@ -173,7 +209,7 @@ cat "$data_file" | ( while read line; do col=0 replace="$( - echo "$line" | sep_line | + csv_split "$line" | while read val; do let col++ echo -n "s/%VAR_${col_name[$col]}%/$(