changed CSV parser
authorChris F.A. Johnson <cfaj@freeshell.org>
Mon, 4 Apr 2011 19:26:19 +0000 (21:26 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 4 Apr 2011 19:26:19 +0000 (21:26 +0200)
generator.sh

index d639780..1595f13 100755 (executable)
@@ -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 <cfaj@freeshell.org> 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]}%/$(