Shell脚本- CSV文件转HTML表格

今天在公司,写了个shell脚本,功能很简单:把csv文件输出为一个HTML页面,方便阅读.

参照文档:

 R1:http://zh.wikipedia.org/wiki/Csv

 R2:http://www.w3school.com.cn/html/html_intro.asp

 R3: http://man.lupaworld.com/content/manage/ringkee/awk.htm

CSV文件如下:(/home/http/php-test/USER_OF_RUN.csv)

USERID,NUM_OF_RUN_ID,STARTDATE,ENDDATE,ISNOTOF_RUN,ORDER_RUN
1,1,"01/01/00 00:00:00","12/31/99 00:00:00",0,
2,1,"01/01/00 00:00:00","12/31/99 00:00:00",0,
3,1,"01/01/00 00:00:00","12/31/99 00:00:00",0,
5,1,"01/01/00 00:00:00","12/31/99 00:00:00",0,
6,1,"01/01/00 00:00:00","12/31/99 00:00:00",0,
7,1,"01/01/00 00:00:00","12/31/99 00:00:00",0,

脚本的主要作用是读取每一行,并且转换为HTML代码的一个表格行,我的脚本如下:

#!/bin/sh
CSVFILE='/home/http/php-test/USER_OF_RUN.csv'
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
-e "</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
do
# echo "$LINE"
create_tr "$LINE"
done <
$CSVFILE
create_table_end
create_html_end

执行完以后,一个CSV文件就输出了HTML文本内容(我这里仅仅输出到终端),该脚本留下自己备查。

顺便给自己另外一个博客做个广告,欢迎来访:http://hellolinux.dyndns.org/

posted @ 2011-05-27 16:08  Hello! Linux 博客  Views(2977)  Comments(0Edit  收藏  举报