shell实现txt转为html

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

shell源码

  1.   
</pre><div class="dp-highlighter bg_python"><div class="bar"><div class="tools"><strong>[python]</strong> <a target=_blank title="view plain" class="ViewSource" href="http://blog.csdn.net/ithomer/article/details/6778166#">view plain</a><a target=_blank title="copy" class="CopyToClipboard" href="http://blog.csdn.net/ithomer/article/details/6778166#">copy</a><a target=_blank title="print" class="PrintSource" href="http://blog.csdn.net/ithomer/article/details/6778166#">print</a><a target=_blank title="?" class="About" href="http://blog.csdn.net/ithomer/article/details/6778166#">?</a></div></div><ol class="dp-py"><li class="alt"><span><span class="comment"># !/bin/sh</span><span>  </span></span></li><li><span>  </span></li><li class="alt"><span>file_input=<span class="string">'txt.log'</span><span>  </span></span></li><li><span>file_output=<span class="string">'txt2html.html'</span><span>  </span></span></li><li class="alt"><span>  </span></li><li><span>td_str=<span class="string">''</span><span>  </span></span></li><li class="alt"><span>  </span></li><li><span>function create_html_head(){  </span></li><li class="alt"><span>    echo -e "<html>  </span></li><li><span>        <body>  </span></li><li class="alt"><span>            <h1>$file_input</h1>"  </span></li><li><span>}  </span></li><li class="alt"><span>  </span></li><li><span>function create_table_head(){  </span></li><li class="alt"><span>    echo -e <span class="string">"<table border="</span><span class="number">1</span><span class="string">">"</span><span>  </span></span></li><li><span>}  </span></li><li class="alt"><span>  </span></li><li><span>function create_td(){  </span></li><li class="alt"><span><span class="comment">#    if [ -e ./"$1" ]; then</span><span>  </span></span></li><li><span>        echo $<span class="number">1</span><span>  </span></span></li><li class="alt"><span>        td_str=`echo $<span class="number">1</span><span> | awk </span><span class="string">'BEGIN{FS="|"}'</span><span class="string">'{i=1; while(i<=NF) {print "<td>"$i"</td>";i++}}'</span><span>`  </span></span></li><li><span>        echo $td_str  </span></li><li class="alt"><span><span class="comment">#    fi</span><span>  </span></span></li><li><span>}  </span></li><li class="alt"><span>  </span></li><li><span>function create_tr(){  </span></li><li class="alt"><span>    create_td <span class="string">"$1"</span><span>  </span></span></li><li><span>    echo -e "<tr>  </span></li><li class="alt"><span>        $td_str  </span></li><li><span>    </tr>" >> $file_output  </span></li><li class="alt"><span>}  </span></li><li><span>  </span></li><li class="alt"><span>function create_table_end(){  </span></li><li><span>    echo -e <span class="string">"</table>"</span><span>  </span></span></li><li class="alt"><span>}  </span></li><li><span>  </span></li><li class="alt"><span>function create_html_end(){  </span></li><li><span>    echo -e <span class="string">"</body></html>"</span><span>  </span></span></li><li class="alt"><span>}  </span></li><li><span>  </span></li><li class="alt"><span>  </span></li><li><span>function create_html(){  </span></li><li class="alt"><span>    rm -rf $file_output  </span></li><li><span>    touch $file_output  </span></li><li class="alt"><span>  </span></li><li><span>    create_html_head >> $file_output  </span></li><li class="alt"><span>    create_table_head >> $file_output  </span></li><li><span>  </span></li><li class="alt"><span>    <span class="keyword">while</span><span> read line  </span></span></li><li><span>    do  </span></li><li class="alt"><span>        echo $line  </span></li><li><span>        create_tr <span class="string">"$line"</span><span>   </span></span></li><li class="alt"><span>    done < $file_input  </span></li><li><span>  </span></li><li class="alt"><span>    create_table_end >> $file_output  </span></li><li><span>    create_html_end >> $file_output  </span></li><li class="alt"><span>}  </span></li><li><span>  </span></li><li class="alt"><span>create_html  </span></li></ol></div><pre class="python" style="display: none;" name="code"># !/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格式:

  1. 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  
  2. 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  
  3. 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  
  4. 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  
  5. 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  
  6. 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  
  7. 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  
  8. 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  
  9. 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  
  10. 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  
  11. 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  
  12. 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  
  13. 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  
  14. 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  
  15. 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  
  16. 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  
  17. 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  
  18. 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  
  19. 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  
  20. 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  
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(源码下载)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值