9

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>

5 Answers 5

14

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).

3
  • Works perfectly to my requirements. Respect. Commented Dec 18, 2013 at 7:16
  • 1
    Images courtesy of debuggex.com Commented Feb 17, 2014 at 20:15
  • Didn't understand, why separate sed processing is needed for header?
    – mtk
    Commented Jan 16, 2022 at 12:32
1

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"
0

Here's a shell script that will convert a CSV to HTML:

http://giantdorks.org/alain/bash-and-awk-to-convert-delimited-data-csv-tsv-etc-to-html-tables/

To address your use case specifically.

Assuming the following original CSV:

$ cat original.csv 
field: 'participation.type', displayName: 'program_type', type: 'String',path:'participation'
field: 'participation.program', displayName: 'program_name', type: 'String',path:'participation'

You may want to modify it slightly:

$ echo field,displayName > modified.csv
$ awk -F"'" 'OFS="," {print$2,$4}' original.csv >> modified.csv

To produce the following cleaned up version:

$ cat modified.csv 
field,displayName
participation.type,program_type
participation.program,program_name

Then running the script linked earlier on the original CSV, will produce the following HTML:

$ csv2htm.sh --head original.csv 
  <table>
    <thead>
      <tr>
        <th>field: 'participation.type'</th>
        <th> displayName: 'program_type'</th>
        <th> type: 'String'</th>
        <th>path:'participation'</th>
      </tr>
    </thead>
      <tr>
        <td>field: 'participation.program'</td>
        <td> displayName: 'program_name'</td>
        <td> type: 'String'</td>
        <td>path:'participation'</td>
      </tr>
  </table>

Running it on the cleaned up CSV, would produce:

$ csv2htm.sh --head modified.csv
  <table>
    <thead>
      <tr>
        <th>field</th>
        <th>displayName</th>
      </tr>
    </thead>
      <tr>
        <td>participation.type</td>
        <td>program_type</td>
      </tr>
      <tr>
        <td>participation.program</td>
        <td>program_name</td>
      </tr>
  </table>
3
  • 1
    The script, at first glance, doesn't seem to handle quoted cell contents (that even might span multiple lines).
    – Anthon
    Commented Feb 17, 2014 at 20:13
  • Right, it does not. Could certainly add quoted cell handling or just preprocess with something like: sed 's/"//g' input Commented Feb 27, 2014 at 20:44
  • Or, if data also contains double quotes: sed 's/^"//;s/"$//;s/","/,/g;' input.csv Commented Feb 27, 2014 at 20:51
0
host=`hostname`
now=`date +"%d-%b-%y"`
now=`echo $now| tr '[a-z]' '[A-Z]'`
yest=`TZ=CST+24 date +%d-%b-%y`
yest=`echo $yest| tr '[a-z]' '[A-Z]'`
sub="Jobs-$host-$now-HealthReport"

if [ -s jobs.csv ]
then
awk 'BEGIN{
FS=","
print "<HTML>""<TABLE border=1 width='100%' align='centre' ><tr bgcolor='#000080'><TH><FONT COLOR='#FFFFFF'>HSCR-DBMSJOB HEALTH REPORT  </TH></FONT>"
print "</TABLE>"
print "<HTML>""<TABLE border=1 width='100%' align='centre' bgcolor='#C6C6C6' BORDERCOLOR='#CCFF00' ><tr bgcolor='#5F9EA0'><TH>SUMMARY</TH>"
print "</TABLE>"
print "<HTML><TABLE border=2 width='100%' align='centre' BORDERCOLOR='#330000' ><trbgcolor='#FFFFCC'>"

print "<TH>BROKEN</TH><TH>SCHEMA_USER</TH><TH>JOB_ID</TH><TH>LAST_DATE</TH><TH>LAST_SEC</TH><TH>THIS_DATE</TH>"
print "<TH>THIS_SEC</TH><TH>NEXT_DATE</TH><TH>NEXT_SEC</TH><TH>NAME</TH>"
}


{
    printf "<TR>"
            for(i=1;i<=10;i++){

                      if(i == 1 && $i == "N" || i == 4 && $i == n || i == 4 && $i == y )
                    {
                            printf "<TD bgcolor='#75923C'>%s</TD>", $i
                    }
                    else if(i == 1 && $i == "Y" || i == 4 && $i != n && $i != y)
                    {
                            printf "<TD bgcolor='#FF0000'>%s</TD>", $i
                    }
                    else
{
                            printf "<TD>%s</TD>", $i
}
            }
            print "</TR>"
    }

END{

            print "</TABLE></BODY></HTML>"
    }' y="$yest" n="$now" jobs.csv > jobstatus-$host-$now.html
else
echo "file not found"

fi
1
  • Can you please reformat your code for better readability. And add some comments... Commented Mar 20, 2017 at 8:48
0

I know it's a late answer to this question, but will help those googling for a solution, for converting bash command output to html table format. There is an easy script available to do this at : https://sourceforge.net/projects/command-output-to-html-table/ which can be used to convert any command output or file to a nice html table format.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .