Convert csv to HTML table
Solution 1
This will do the trick:
echo "<table>" ;
print_header=true
while read INPUT ; do
if $print_header;then
echo "<tr><th>$INPUT" | sed -e 's/:[^,]*\(,\|$\)/<\/th><th>/g'
print_header=false
continue
fi
echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" ;
done < Medical.csv ;
echo "</table>"
Explanation of the regex used is sed
:
:[^,]*(,|$)
This will match : 'participation.type',
and :'participation'\n
($
means end of input/line in regex).
Solution 2
If you have sqlite and the command-line program sqlite3
, you can run that program and then:
.import --csv input.csv tmp
.mode html
.output output.html
select * from tmp;
You may need to add something to the resulting HTML file, e.g.
<!doctype html>
<table>
so that it is recognized by browsers.
As a bash script, this could be
#!/bin/bash
TMP=`mktemp csv.XXXXX`
trap "rm -f $TMP" EXIT
(echo .import "$1" tmp; echo .mode html; echo .output "$TMP"; echo 'select * from tmp;' ) | sqlite3
awk 'BEGIN{print "<!doctype html><table>"};{print}' < "$TMP" > "$2"
Related videos on Youtube
prayagupa
(def summary[] (:TCP/IP-socket-programmer "who loves to send and receive bits and bytes") (:using "bytecode instructions which runs on JVM [clojure, groovy, java12]") (and ([sql, nosql])) (also (did [PHP, CLR] socket programming once upon a time)) (does mobile app programming sometimes in [Android] Platform.) (TDD practitioner)) (def resume[] {:stackoverflow_careers "http://careers.stackoverflow.com/prayagupd" ))
Updated on September 18, 2022Comments
-
prayagupa over 1 year
I have a
Medical.csv
file with rows of following format,field: 'participation.type', displayName: 'program_type', type: 'String',path:'participation' field: 'participation.program', displayName: 'program_name', type: 'String',path:'participation'
I want to write a bash script to convert it to HTML table with
field
,displayName
andtype
as headers dynamically.The
Csv2HtmlConverter.sh
(Inspired by answer at Convert csv to html table using) isecho "<table>" ; while read INPUT ; do echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" ; done < Medical.csv ; echo "</table>"
The result for above script is as below which is fine to some extent but I want to add
<th>field</th>
,<th>displayName</th>
dynamically.<table> <tr><td>field: 'participation.type'</td><td> displayName: 'program_type'</td><td> type: 'String'</td><td>path:'participation'</td></tr> <tr><td>field: 'participation.program'</td><td> displayName: 'program_name'</td><td> type: 'String'</td><td>path:'participation'</td></tr> </table>
-
prayagupa over 10 yearsWorks perfectly to my requirements. Respect.
-
Anthon about 10 yearsThe script, at first glance, doesn't seem to handle quoted cell contents (that even might span multiple lines).
-
Stéphane Chazelas about 10 yearsImages courtesy of debuggex.com
-
Alain Kelder about 10 yearsRight, it does not. Could certainly add quoted cell handling or just preprocess with something like:
sed 's/"//g' input
-
Alain Kelder about 10 yearsOr, if data also contains double quotes:
sed 's/^"//;s/"$//;s/","/,/g;' input.csv
-
Atul Vekariya about 7 yearsCan you please reformat your code for better readability. And add some comments...
-
ekoeppen over 2 yearsDidn't understand, why separate sed processing is needed for header?