文档介绍:IT-Homer 专栏
成功是优点的发挥,失败是缺点的积累! 不为失败找理由,只为成功找
方法……
shell 实现txt转换成html
分类: Linux 2011-09-15 14:34 593人阅读评论(0) 收藏举报
原理: awk命令,分割格式化的txt(txt文件格式以“|”分割开的)成数组,然后拼接成html格式(html - head
- title - body - table)
shell源码
# !/bin/sh
file_input=''
file_output=''
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
1
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||887,058|10,000,000 - 50,000,000|Free|August 30, 2011||19M| and up|Low Maturity
Angry Birds Seasons|Arcade & Action||314,060|10,000,000 - 50,000,000|Free|September 1, 2011||22M| and up|Low Maturity
Bunny Shoot