使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:51:42  来源:igfitidea点击:

convert a text file output to a html format using bash

bashawk

提问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.htmlbut 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 \nneed 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