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

所属分类: 脚本专栏 / linux shell 阅读数: 860
收藏 0 赞 0 分享

原理: 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(源码下载)

更多精彩内容其他人还在看

linux创建用户useradd命令代码示例

本文通过代码示例给大家介绍了adduser与useradd命令二者的关系 以及使用useradd命令添加用户的方法,需要的朋友参考下吧
收藏 0 赞 0 分享

Linux 中LVS NAT 配置步骤的详解

这篇文章主要介绍了Linux 中LVS NAT 配置步骤的详解的相关资料,这里列出详细的实现步骤,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux中的内核链表实例详解

这篇文章主要介绍了Linux中的内核链表实例详解的相关资料,链表中一般都要进行初始化、插入、删除、显示、释放链表,寻找节点这几个操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell中特殊字符的用法总结大全

这篇文章主要给大家总结了关于Shell中特殊字符的相关资料,文中包括分好、&、#、!、$、大于号、单双引号等等一系列特殊字符的用法,通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Shell脚步攻略之管道重定向基础

管道是为了解决进程间通信问题而存在,它可以让两个进程之间的数据进行传递,将一个进程的输出数据传递给另一个进程作为其输入数据
收藏 0 赞 0 分享

Linux shell数组循环的实例详解

这篇文章主要介绍了Linux shell数组循环的实例详解的相关资料,这里举例说明如何实现shell数组循环,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell内置判断语句

内置判断,成功的时候返回0,不成功返回非零。接下来通过本文重点给大家介绍linux shell内置判断语句,感兴趣的的朋友一起看看吧
收藏 0 赞 0 分享

Linux文件的归档和压缩命令

文件归档命令tar,文件归档有好多好处,方便使用易于管理,接下来通过本文给大家分享linux文件的归档和压缩命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Linux Shell中curl和wget使用代理IP的方法教程

这篇文章主要给大家介绍了关于在Linux Shell中curl和wget使用代理IP的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Linux 日常常用指令及应用小结

最近在学习一些基本的Linux指令,在这里总结一下,在搭环境中常用的一些指令,熟悉这些指令就基本能够使用CentOS进行日常操作了
收藏 0 赞 0 分享
查看更多