Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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>
share|improve this question

2 Answers 2

up vote 5 down vote accepted

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

share|improve this answer
    
Works perfectly to my requirements. Respect. –  Prayag Upd Dec 18 '13 at 7:16
1  
Images courtesy of debuggex.com –  Stéphane Chazelas Feb 17 at 20:15

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>
share|improve this answer
1  
The script, at first glance, doesn't seem to handle quoted cell contents (that even might span multiple lines). –  Anthon Feb 17 at 20:13
    
Right, it does not. Could certainly add quoted cell handling or just preprocess with something like: sed 's/"//g' input –  Alain Kelder Feb 27 at 20:44
    
Or, if data also contains double quotes: sed 's/^"//;s/"$//;s/","/,/g;' input.csv –  Alain Kelder Feb 27 at 20:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.