博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell 实现txt转换成html
阅读量:2387 次
发布时间:2019-05-10

本文共 4263 字,大约阅读时间需要 14 分钟。

shell源码

  1. # !/bin/sh  

  2.   

  3. file_input='txt.log'  

  4. file_output='txt2html.html'  

  5.   

  6. td_str=''  

  7.   

  8. function create_html_head(){  

  9.     echo -e "<html>  

  10.         <body>  

  11.             <h1>$file_input</h1>"  

  12. }  

  13.   

  14. function create_table_head(){  

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

  16. }  

  17.   

  18. function create_td(){  

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

  20.         echo $1  

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

  22.         echo $td_str  

  23. #    fi  

  24. }  

  25.   

  26. function create_tr(){  

  27.     create_td "$1"  

  28.     echo -e "<tr>  

  29.         $td_str  

  30.     </tr>" >> $file_output  

  31. }  

  32.   

  33. function create_table_end(){  

  34.     echo -e "</table>"  

  35. }  

  36.   

  37. function create_html_end(){  

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

  39. }  

  40.   

  41.   

  42. function create_html(){  

  43.     rm -rf $file_output  

  44.     touch $file_output  

  45.   

  46.     create_html_head >> $file_output  

  47.     create_table_head >> $file_output  

  48.   

  49.     while read line  

  50.     do  

  51.         echo $line  

  52.         create_tr "$line"   

  53.     done < $file_input  

  54.   

  55.     create_table_end >> $file_output  

  56.     create_html_end >> $file_output  

  57. }  

  58.   

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

转载地址:http://wmsab.baihongyu.com/

你可能感兴趣的文章
Qt C++中的关键字explicit .
查看>>
qtcreator中常用快捷键
查看>>
PowerDesigner 简介
查看>>
VS2008快捷键大全
查看>>
Access 操作或事件已被禁用模式阻止
查看>>
C# 控件置于最顶层、最底层
查看>>
几个常见的压缩算法
查看>>
浮点数的存储
查看>>
点到线段的距离
查看>>
HTML_5_Canvas
查看>>
NuGet学习笔记(1)——初识NuGet及快速安装使用
查看>>
NuGet学习笔记(2)——使用图形化界面打包自己的类库
查看>>
C# 数据类型基础,堆栈,装箱与拆箱
查看>>
HTML 中的<div>
查看>>
C#中的static、readonly与const的比较
查看>>
C# ListView用法详解
查看>>
搜索自己博客中的文章
查看>>
出色图形用户界面(GUI)设计规范(下)
查看>>
C# winform 实现选择文件夹对话框
查看>>
c# 获取相对路径
查看>>