Convert csv to HTML table

43,080

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:

:[^,]*(,|$)

Regular expression visualization

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"
Share:
43,080

Related videos on Youtube

prayagupa
Author by

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, 2022

Comments

  • prayagupa
    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 and type as headers dynamically.

    The Csv2HtmlConverter.sh (Inspired by answer at Convert csv to html table using) is

        echo "<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
    prayagupa over 10 years
    Works perfectly to my requirements. Respect.
  • Anthon
    Anthon about 10 years
    The script, at first glance, doesn't seem to handle quoted cell contents (that even might span multiple lines).
  • Stéphane Chazelas
    Stéphane Chazelas about 10 years
    Images courtesy of debuggex.com
  • Alain Kelder
    Alain Kelder about 10 years
    Right, it does not. Could certainly add quoted cell handling or just preprocess with something like: sed 's/"//g' input
  • Alain Kelder
    Alain Kelder about 10 years
    Or, if data also contains double quotes: sed 's/^"//;s/"$//;s/","/,/g;' input.csv
  • Atul Vekariya
    Atul Vekariya about 7 years
    Can you please reformat your code for better readability. And add some comments...
  • ekoeppen
    ekoeppen over 2 years
    Didn't understand, why separate sed processing is needed for header?