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
add comment

1 Answer

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
add comment

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.