天天看點

Shell腳本CSV2HTML表格

網上看了一個不錯的腳本,改了一點點,記錄一下,

#!/bin/sh 

CSVFILE= 

while getopts c: opt 

do 

        case $opt in 

                c) CSVFILE=$OPTARG 

                ;; 

                '?') echo "Usage: -c csv file name" 

        esac 

done 

TD_STR="" 

#this function create a <td> block 

create_td() 

  #echo $1 

  TD_STR=`echo $1 | awk 'BEGIN{FS=","}{i=1; while(i<=NF) {print "<td>"$i"</td>";i++}}'` 

#this function create a row html script(<tr>block). 

create_tr() 

  create_td "$1" 

  echo -e "<tr>\n$TD_STR\n<tr/>\n" 

#create html script head 

create_html_head() 

  echo -e "<html>\n<body>\n<h1>$CSVFILE</h1>\n" 

#create html script end 

create_html_end() 

  echo '</body></html>' 

create_table_head() 

  echo -e "<table border="1">\n" 

create_table_end() 

   echo -e "</table>\n" 

create_html_head 

create_table_head 

while read LINE 

 # echo "$LINE" 

  create_tr "$LINE" 

done < $CSVFILE 

create_table_end 

create_html_end 

本文轉自 nonono11 51CTO部落格,原文連結:http://blog.51cto.com/abian/1128227,如需轉載請自行聯系原作者