使用 bash 将文本文件输出转换为 html 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22285383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert a text file output to a html format using bash
提问by Sathish
HI am trying to convert a text file to html with table so that i could mail the output in a table format and i used awk 'BEGIN{print "Content-Type: text/html; charset="us-ascii""\n "<html>"\n "<Body>"\n "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print \n</Body>"\n "</html>"\n"</table>"}' a.txt >> email.html
but am having problems could some one help me on this
嗨,我正在尝试将文本文件转换为带有表格的 html,以便我可以以表格格式邮寄输出,我使用过awk 'BEGIN{print "Content-Type: text/html; charset="us-ascii""\n "<html>"\n "<Body>"\n "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print \n</Body>"\n "</html>"\n"</table>"}' a.txt >> email.html
但遇到了问题,有人可以帮我解决这个问题
回答by Sathish
-Edited-It works with this:
- 编辑 -它适用于:
awk '
BEGIN{
print "Content-Type: text/html; charset="us-ascii"\n<html>\n<head>\n<style>\ntable , th,td\n{\n border:1px solid black;
border-collapse:collapse;\n}\n</style>\n</head>\n<Body>\n<table>"
}
{print "<tr>"
for(i=1;i<=NF;i++)
print "<td>" $i"</td>"
print "</tr>"
}
END{
print "\n</table>\n</Body>\n</html>\n"
}' a.txt >> email.html
回答by Jotne
You need to clean your line. The \n
need to be in double quote like this:
你需要清理你的线路。将\n
需要在这样的双引号:
awk '
BEGIN{
print "Content-Type: text/html; charset=us-ascii\n <html>\n <Body>\n<table>"
}
{print "<tr>"
for(i=1;i<=NF;i++)
print "<td>" $i"</td>"
print "</tr>"
}
END{
print "\n</Body>\n</html>\n</table>"
}' a.txt >> email.html