欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2022-07-04 18:28:53
原理: awk命令,分割格式化的txt(txt文件格式以“|”分割开的)成数组,然后拼接成html格式(html - head - title - body - table...

原理: 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:

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