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

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索html
, txt
转换
linux c语言实现shell、linux shell脚本实现、linux shell实现、shell实现xls转换csv、linux shell源代码,以便于您获取更多的相关知识。

时间: 2024-08-04 05:00:09

linux shell txt转换成html的实现代码_linux shell的相关文章

Linux oracle数据库自动备份自动压缩脚本代码_linux shell

复制代码 代码如下: #!#backup.sh##系统名称sysname=gzsyspath=/home/oracle/databak/$sysname/v_date=$(date '+%Y%m%d%H%M%S')logfile=$syspath/backup.$v_date.logecho backup_time>>$logfiledate '+%Y-%m-%d %H:%M:%S' >> $logfile#数据库使用情况echo "backup gznxt"&

Linux Shell中判断进程是否存在的代码_linux shell

1 利用pgrep 匹配名字 复制代码 代码如下: if test $( pgrep -f $1 | wc -l ) -eq 0 then echo "进程不存在" else echo "存在进程" fi 以下是补充内容: 当前系统中的进程: apple@ubuntu:~$ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 13:57 ? 00:00:02 /sbin/init root 2 0 0 13:57

xml-C# 如何将纯文本TXT转换成XML格式

问题描述 C# 如何将纯文本TXT转换成XML格式 想做一个小的demo 学习中遇到一点小问题 请大神指导 通过一个简单的转换 不求转换精准 解决方案 public partial class FrmTxtXml : Form { String txtContent = String.Empty; // public FrmTxtXml() { InitializeComponent(); } //打开txt文件 private void toolStripOpen_Click(object s

求代码 代码-求用C++写任意整数转换成中文大写的代码

问题描述 求用C++写任意整数转换成中文大写的代码 我正在学习C++,才开始不懂怎么写,还没有理清头绪,希望有人能帮我写一下,我好有一个指路灯,谢谢 解决方案 CString ChineseCapitalMoney(double Num) { CString szChMoney,szNum; int iLen, iNum, iAddZero=0; TCHAR* hzUnit[18]={_T("分"),_T("角"),_T("元"),_T(&quo

急,急,急~!!如何在C#中将汉字转换成2进制代码并将2进制代码存放在一个数组里面

问题描述 我有个设计需要将文本中的汉字转换成2进制代码然后存放在一个BYTE型的数组里面,希望各位大大帮我解决下. 解决方案 解决方案二:strings="汉字";byte[]b=System.Text.Encoding.Default.GetBytes(s);解决方案三:下面是转化成字符串形式的16位二进制publicstaticstringChar2Binary(stringstr){stringstrCode="";byte[]byteArray=null;f

python中将阿拉伯数字转换成中文的实现代码_python

复制代码 代码如下: #!/usr/bin/python #-*- encoding: utf-8 -*- import types class NotIntegerError(Exception): pass class OutOfRangeError(Exception): pass _MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', ) _P0 = (u'', u'十', u'百', u'千', )

如何将txt转换成图片

一.准备工作 1.在电脑中下载安装迅捷PDF转换成Word转换器. 2.将txt文件中的内容复制到Word文件中,因为该转换器支持Word转图片. 二.开始转换 第一步,打开迅捷PDF转换成Word转换器,大家可以看到软件有10种转换模式,这里我们需要选择"文件转IMG"转换模式. 第二步,点击"添加文件"按钮,将需要转换的文件添加到软件中.大家也可以使用鼠标将文件拖拽到软件界面中进行添加. 第三步,选择转换文件的存储路径.默认情况下软件是将文件保存在桌面上,这里我

txt转换成bin文件-怎么将txt格式文件转换成bin文件?有重赏

问题描述 怎么将txt格式文件转换成bin文件?有重赏 我是一名工科学生,实验中一个仪器测试的数据是bin格式文件,仪器自带有一个bin文件转换成txt文件的功能,我的实验数据都已经被我转换成了txt文件,有一天件我同学帮我装电脑系统全部格式化硬盘了,我的原始的bin文件全没了,没有备份.现在自己的导师要看我原始bin文件格式的数据,不相信我的txt格式数据,所以我想问问各位大神,有没有什么软件可以将我的txt格式文件转换成原来的bin文件,且仪器能识别的bin文件,小弟愿意给现金作为报答,小弟

Linux系统中bash shell编程的10个基础问题讲解_linux shell

第1问:为何叫做shell?在介绍 shell 是什么东西之前,不妨让我们重新审视使用者与电脑的关系.我们知道电脑的运作不能离开硬件,但使用者却无法直接对硬件作驱动,硬件的驱动只能透过一个称为"操作系统(Operating System)"的软件来控管,事实上,我们每天所谈的linux,严格来说只是一个操作系统,我们称之为"核心(kernel)".然而,从使用者的角度来说,使用者也没办法直接操作kernel,而是透过kernel的"外壳"程序,也