Permalink
Browse files

Convert csv to html

  • Loading branch information...
1 parent a1fc236 commit 27a6df881c2b2e7be7f57f1ca2e556ffc8866868 @Gcool committed Apr 19, 2012
Showing with 48 additions and 0 deletions.
  1. +48 −0 csv2html.sh
View
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+NUMFIELDS=0
+OUT=()
+TLC=0
+
+to_html() {
+ local lc=0
+
+ while IFS=',' read -r -a items; do
+ (( ++lc ))
+ OUT+=("\t<tr>\n")
+
+ (( ${#items[@]} == 0 )) && continue
+
+ (( NUMFIELDS == 0 )) && NUMFIELDS=${#items[@]}
+ if (( NUMFIELDS != ${#items[@]} )); then
+echo "error: Malformed CSV ($file: line $LC). Expected $NUMFIELDS fields, found ${#items[@]}." >&2
+ exit 1
+ fi
+
+ for item in "${items[@]}"; do
+OUT+=("\t\t<td>${item/==NULL==/}</td>\n")
+ done
+
+OUT+=("\t</tr>\n")
+
+ (( TLC += lc ))
+
+ done < <(sed 's/^,/==NULL==,/;
+s/,,/,==NULL==,/g;
+s/,$/,==NULL==/' <&6)
+}
+
+OUT+=("<table>\n")
+
+if (( $# == 0 )); then
+exec 6<&0
+ to_html
+else
+for file; do
+exec 6< "$file"
+ to_html
+ exec 6<&-
+ done
+fi
+
+(( TLC > 0 )) && OUT+=("</table>\n")

0 comments on commit 27a6df8

Please sign in to comment.