您的位置:首页 > 运维架构 > Shell

shell 实现txt转换成html

2015-02-09 14:57 411 查看
# !/bin/sh

file_input='txt.log'

file_output='txt2html.html'

td_str=''

function create_html_head(){

echo -e "<html>

<body>

<h1>$file_input</h1>"

}

function create_table_head(){

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

}

function create_td(){

# if [ -e ./"$1" ]; then

echo $1

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

echo $td_str

# fi

}

function create_tr(){

create_td "$1"

echo -e "<tr>

$td_str

</tr>" >> $file_output

}

function create_table_end(){

echo -e "</table>"

}

function create_html_end(){

echo -e "</body></html>"

}

function create_html(){

rm -rf $file_output

touch $file_output

create_html_head >> $file_output

create_table_head >> $file_output

while read line

do

echo $line

create_tr "$line"

done < $file_input

create_table_end >> $file_output

create_html_end >> $file_output

}

create_html

cat cvshtml-common.sh
#!/bin/sh
#通用cvs转html脚本
#调用格式:./cvshtml-common.sh "列名1,列名2,列名3..." 文件名
CSVFILE=$2
TITLE=$1
TD_STR=''

#this function create a <td> block
create_td()
{
#TD_STR2=`echo $1 | awk -F, '{if($6==1 || $7==1)for(i=1;i<=NF;i++)print "<td align=center bgColor=#FF33FF >"$i"</td>"; else for(i=1;i<=NF;i++)print "<td align =center >"$i"</td>"}'`
#TD_STR3=`echo $1 | awk 'BEGIN{FS=","}{i=1; while(i<=NF) {print "<td align=center bgColor=#EEEEEE >"$i"</td>";i++}}'`
count=$(($2%2))
if [[ $count -eq 0 ]]; then
TD_STR=`echo $1 | awk -F, '{for(i=1;i<=NF;i++)print "<td align=center bgColor=#CCFFFF >"$i"</td>";}'`
else
TD_STR=`echo $1 | awk -F, '{for(i=1;i<=NF;i++)print "<td align=center bgColor=#F5FAFA >"$i"</td>";}'`
fi
}
#this function create a row html script(<tr>block).
create_tr()
{
create_td $1 $2
echo "<tr>$TD_STR</tr>"
}
#create html script head
create_html_head()
{
echo "<html><body>"
}
#create html script end
create_html_end()
{
echo "</body></html>"
#echo "<table align=center></table>"
}
create_table_head()
{
echo "<table border=1 cellspacing="0" borderColor=#C1DAD7 width=1060 align=left >"
#echo "<td align=center bgColor=#ff9900>(免费)日期</td>"
TR_TITLE=`echo $TITLE|awk -F, '{for(i=1;i<=NF;i++)print "<td align=center bgColor=#00B0F0 >"$i"</td>";}'`
echo "<tr>$TR_TITLE</tr>"
}
create_table_end()
{
echo "</table>"
}
create_html_head
create_table_head
line_count=1;
while read LINE
do
#echo "$LINE"
line_count=$(($line_count+1))
create_tr $LINE $line_count
done < $CSVFILE
create_table_end
create_html_end

cat /home/haoren/xiaofei-paihang-cvshtml2.sh
#!/bin/sh
CSVFILE=$1
TD_STR=''
day2=`date +"%Y-%m-%d"`
day3=`date -d"1 days ago" +%Y-%m-%d`
day4=`date -d"1 years ago" +%Y-%m-%d`

#this function create a <td> block
create_td()
{
TD_STR2=`echo $1 | awk -F, '{if($6==1 || $7==1)for(i=1;i<=NF;i++)print "<td align=center bgColor=#FF33FF >"$i"</td>"; else for(i=1;i<=NF;i++)print "<td align =center >"$i"</td>"}'`
TD_STR3=`echo $1 | awk 'BEGIN{FS=","}{i=1; while(i<=NF) {print "<td align=center bgColor=#EEEEEE >"$i"</td>";i++}}'`
}
#this function create a row html script(<tr>block).
create_tr()
{
create_td "$1"
# echo "<tr>$TD_STR1"
#echo "<tr>$TD_STR2</tr>"
echo "<tr>$TD_STR3</tr>"
}
#create html script head
create_html_head()
{
#echo "<table width=500 align=center >"
#echo "<td <font color=blue align=right>`date "+%Y%m%d"`</td>"
#echo "</table>"
#echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
#echo '<html xmlns="http://www.w3.org/1999/xhtml">'
#echo '<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>赛车统计</title></head>'
#echo "<body>"
#echo "<p>下面是赛车数据 </p>"
echo "<html><body>"
echo "<table border=1 borderColor=#ff9900 width=1000 align=left >"
# echo "<td align=center bgColor=#ff9900>ID</td>"
echo "<td align=center bgColor=#ff9900>排行</td>"
echo "<td align=center bgColor=#ff9900>消费号</td>"
echo "<td align=center bgColor=#ff9900>收费票</td>"
echo "<td align=center bgColor=#ff9900>免费票</td>"
echo "<td align=center bgColor=#ff9900>绰号</td>"
}
#create html script end
create_html_end()
{
echo "</body></html>"
echo "<table align=center></table>"
}
create_table_head()
{
echo #"<table border="1">"
}
create_table_end()
{
echo "</table>"
}
create_html_head
create_table_head
while read LINE
do
# echo "$LINE"
create_tr "$LINE"
done < $CSVFILE
create_table_end
create_html_end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息