linux中shell如何實(shí)現(xiàn)txt轉(zhuǎn)換成html-創(chuàng)新互聯(lián)

小編給大家分享一下linux中shell如何實(shí)現(xiàn)txt轉(zhuǎn)換成html,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)公司為您提適合企業(yè)的網(wǎng)站設(shè)計(jì)?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強(qiáng)的網(wǎng)絡(luò)競(jìng)爭(zhēng)力!結(jié)合企業(yè)自身,進(jìn)行網(wǎng)站設(shè)計(jì)及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到成都網(wǎng)站建設(shè)、網(wǎng)站制作, 我們的網(wǎng)頁設(shè)計(jì)師為您提供的解決方案。

原理: awk命令,分割格式化的txt(txt文件格式以“|”分割開的)成數(shù)組,然后拼接成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>
      <h2>$file_input</h2>"
}

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

測(cè)試的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&reg; 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如何實(shí)現(xiàn)txt轉(zhuǎn)換成html

以上是“l(fā)inux中shell如何實(shí)現(xiàn)txt轉(zhuǎn)換成html”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前文章:linux中shell如何實(shí)現(xiàn)txt轉(zhuǎn)換成html-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://muchs.cn/article44/djhcee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站品牌網(wǎng)站制作、標(biāo)簽優(yōu)化搜索引擎優(yōu)化、網(wǎng)站營銷、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司