`
king_tt
  • 浏览: 2109994 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

shell 实现txt转换成html

 
阅读更多

原理: awk命令,分割格式化的txt(txt文件格式以“|”分割开的)成数组,然后拼接成html格式(html - head - title - body - table)

shell源码


# !/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


测试的txt格式:

Angry Birds|Arcade & Action|4.6|887,058|10,000,000 - 50,000,000|Free|August 30, 2011|1.6.3|19M|1.6 and up|Low Maturity
Angry Birds Seasons|Arcade & Action|4.5|314,060|10,000,000 - 50,000,000|Free|September 1, 2011|1.6.0|22M|1.6 and up|Low Maturity
Bunny Shooter Free Game|Brain & Puzzle|4.9|121,579|1,000,000 - 5,000,000|Free|September 7, 2011|1.06|8.6M|2.1 and up|Low Maturity
Angry Birds Rio|Arcade & Action|4.7|310,324|10,000,000 - 50,000,000|Free|August 29, 2011|1.3.0|17M|1.6 and up|Everyone
Words With Friends Free|Brain & Puzzle|3.7|312,017|10,000,000 - 50,000,000|Free|September 1, 2011|Varies with device|Varies with device|2.1 and up|Everyone
TETRIS® free|Brain & Puzzle|3.8|1,288|500,000 - 1,000,000|Free|September 1, 2011|1.0.27|8.7M|1.6 and up|Low Maturity
Drag Racing|Racing|4.5|150,279|10,000,000 - 50,000,000|Free|September 9, 2011|1.1.3|6.5M|1.6 and up|Everyone
Drunk Man|Racing|3.6|2,388|1,000,000 - 5,000,000|Free|September 2, 2011|1.2.1|998k|1.5 and up|Everyone
Solitaire|Cards & Casino|4.3|83,548|10,000,000 - 50,000,000|Free|December 22, 2010|1.12.2|83k|1.0 and up|Everyone
Dragon, Fly!|Arcade & Action|4.6|46,790|1,000,000 - 5,000,000|Free|September 3, 2011|1.8|3.2M|1.6 and up|Low Maturity
Pimple Popper|Arcade & Action|2.7|3,014|1,000,000 - 5,000,000|Free|September 8, 2011|1.8|2.2M|2.0 and up|Low Maturity
Fruit Ninja Free|Arcade & Action|4.5|13,915|1,000,000 - 5,000,000|Free|August 4, 2011|1.6.2.10|18M|2.1 and up|Low Maturity
Fruit Slice|Arcade & Action|4.5|165,603|10,000,000 - 50,000,000|Free|September 14, 2011|1.3.2|4.0M|1.6 and up|Everyone
Prize Claw|Arcade & Action|3.9|1,102|500,000 - 1,000,000|Free|September 2, 2011|1.1|13M|2.0.1 and up|Everyone
3D Bowling|Arcade & Action|4.0|14,794|5,000,000 - 10,000,000|Free|June 28, 2011|1.3|9.8M|2.0.1 and up|Everyone
7 Little Words|Brain & Puzzle|4.8|21,073|500,000 - 1,000,000|Free|August 10, 2011|1.00|3.2M|2.2 and up|Everyone
Third Blade|Arcade & Action|4.3|6,475|500,000 - 1,000,000|Free|September 9, 2011|1.0.2|49M|1.6 and up|Medium Maturity
Shoot Bubble Deluxe|Arcade & Action|4.2|11,645|5,000,000 - 10,000,000|Free|May 28, 2011|2.5|1.1M|1.1 and up|Everyone
Racing Moto|Arcade & Action|4.4|79,829|1,000,000 - 5,000,000|Free|August 20, 2011|1.1.2|3.9M|1.6 and up|Everyone
Zynga Poker|Cards & Casino|4.6|91,976|1,000,000 - 5,000,000|Free|August 31, 2011|Varies with device|Varies with device|2.0.1 and up|Medium Maturity

生成的html:



shell 实现txt转换成html(源码下载)


分享到:
评论

相关推荐

    shell 实现txt转换成html(含shell脚本和测试样例)

    shell 实现txt转换成html(含shell脚本和完整的测试样例)

    linux shell txt转换成html的实现代码

    主要介绍了linux shell txt转换成html的实现代码,需要的朋友可以参考下

    人脸识别代码

    &lt;img src="http://affluent.oss-cn-hangzhou.aliyuncs.com/html/images/dface_logo.png" width="350"&gt; ...如果使用wider face的 wider_face_train.mat 注解文件需要转换成txt格式的,我这里用h5py写了个 [转换脚本]...

    Delphi 7 新概念百例

    32 用剪贴板复制和粘贴图像 │ └─31 实现.dfm文件和.txt文件的互相转换 ├─3第三节 Shell研究 │ ├─30 为程序创建快捷方式 │ ├─29 获取Windows的若干特殊文件夹路径(二) │ ├─28 获取Windows的若干特殊...

    网管教程 从入门到精通软件篇.txt

    网管教程 从入门到精通软件篇 ★一。★详细的xp修复控制台命令和用法!!! 放入xp(2000)的光盘,安装时候选R,修复! Windows XP(包括 Windows 2000)的控制台命令...JSP:HTML网页,其中包含有对一个Java servlet...

    易语言 茶凉专用模块

    子程序 八到十, 整数型, 公开, 将八进制转换成十进制(返回十进制整数) .参数 八进制文本, 文本型, , 欲转换的八进制文本 .子程序 彩色字体, , 公开, 彩色动态字体 感谢[梦飞鸟] .参数 内容, 文本型 .参数 窗口句柄,...

    JAVA上百实例源码以及开源项目

    util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码加以...

    JAVA上百实例源码以及开源项目源代码

    util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码加以...

    cmd操作命令和linux命令大全收集

    3. Nslookup-------IP地址侦测器 ,是一个 监测网络中 DNS 服务器是否能正确实现域名解析的命令行工具。它在 Windows NT/2000/XP 中均可使用,但在 Windows 98 中却没有集成这一个工具。 4. explorer-------打开...

    js使用小技巧

    快速转到位置 obj.scrollIntoView(true) 锚 &lt;a name="first"&gt; &lt;a href="#first"&gt;anchors 网页传递参数 location.search(); 可编辑 obj.contenteditable=true 执行菜单命令 obj.execCommand 双字节字符 ...

    计算机应用技术(实用手册)

    4.格式之间的转换,使用winavi进行转换 32 5.常用的快捷键,使用快捷可以帮助我节约时间。 33 6.开机常按F8可以进入安全模式或是带DOS命令的安全模式。 33 第六章GHOST的备份与恢复 34 第七章 综合应用 44 一....

Global site tag (gtag.js) - Google Analytics