将CSV转换为HTML表


8

我有一个Medical.csv文件,其行格式如下,

    field: 'participation.type', displayName: 'program_type', type: 'String',path:'participation'
    field: 'participation.program', displayName: 'program_name', type: 'String',path:'participation'

我想编写一个bash脚本,以将其动态转换为HTML表fielddisplayNametype作为标题。

Csv2HtmlConverter.sh(通过在回答灵感转换CSV HTML表格使用到)是

    echo "<table>" ;
    while read INPUT ; do
            echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" ;
    done < Medical.csv ;
    echo "</table>"

对于上面的脚本的结果如下这是罚款,在一定程度上,但我想补充<th>field</th><th>displayName</th>动态。

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

Answers:


12

这将达到目的:

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

正则表达式的解释是sed

:[^,]*(,|$)

正则表达式可视化

这将匹配: 'participation.type',:'participation'\n$表示正则表达式中输入/行的结尾)。


完全可以满足我的要求。尊重。
2013年


0

这是一个将CSV转换为HTML的Shell脚本:

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

具体解决您的用例。

假设以下原始CSV:

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

您可能需要稍作修改:

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

产生以下清理版本:

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

然后运行先前在原始CSV上链接的脚本,将产生以下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>

在清理的CSV上运行它会产生:

$ 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>

1
乍一看,该脚本似乎无法处理引用的单元格内容(甚至可能跨越多行)。
Anthon

是的,事实并非如此。当然可以添加引用的单元格处理或仅进行以下预处理:sed 's/"//g' input
Alain Kelder 2014年

或者,如果数据还包含双引号:sed 's/^"//;s/"$//;s/","/,/g;' input.csv
Alain Kelder 2014年

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

您能否重新格式化代码以提高可读性。并添加一些评论...
Romeo Ninov

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.