如何使用AWK从文件读取并输出到html表

如何使用AWK从文件读取并输出到html表

本文介绍了如何使用AWK从文件读取并输出到html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AWK的newby,我知道这不是一个具体问题.我只需要一些建议,我应该怎么做.

I'm newby in AWK and I know its not a specific question. I just need some advice how should i do this.

在文件 author.list 中给出以下名称:

Given the following names in a file, author.list:


KOVACS PETER
Kiss Roland
Nagy jolan
Lisztes Tibor
Feher aNDRas
Korma Maria
Akarki Jack

编写一个AWK程序,该程序可以从文件中读取名称并将其打印到输出文件 output.html中具有三列格式的html表中.:

write an AWK program that can read the names from the file and print them to an html table with a three-column format in an output file, output.html. The table should render like this:

    Kovacs Peter     Lisztes Tibor    Akarki Jack
    Kiss Roland      Feher Andras
    Nagy Jolan       Korma Maria

执行示例:

awk -f convert.awk author.list > output.html

确保 output.html 是有效的html文件.

Ensure that output.html is a valid html file.

推荐答案

没有看到要生成的HTML,这只是一个猜测,但这可能就是您想要的:

Without seeing the HTML you want to generate it's a guess but this might be what you want:

$ cat tst.awk
BEGIN {
    print "<html>"
    print "  <table>"
}
{
    for (i=1; i<=NF; i++) {
        $i = toupper(substr($i,1,1)) tolower(substr($i,2))
    }
    if ( (NR%3) == 1 ) {
        if (NR>1) print "      </tr>"
        print "      <tr>"
    }
    printf "        <td>%s</td>\n", $0
}
END {
    for (i=NR+1; (i%3) != 1; i++) {
        printf "        <td>%s</td>\n", ""
    }
    print "      </tr>"

    print "  </table>"
    print "</html>"
}

.

$ awk -f tst.awk author.list
<html>
  <table>
      <tr>
        <td>Kovacs Peter</td>
        <td>Kiss Roland</td>
        <td>Nagy Jolan</td>
      </tr>
      <tr>
        <td>Lisztes Tibor</td>
        <td>Feher Andras</td>
        <td>Korma Maria</td>
      </tr>
      <tr>
        <td>Akarki Jack</td>
        <td></td>
        <td></td>
      </tr>
  </table>
</html>

名称的大写/小写转换将不仅在 McDonald O'Hara Billy-Bob 之类的名称上失败名称开头应有1个大写字母.如果必须处理该问题,则需要提供一种算法.

The name upper/lower case conversion will fail on names like McDonald or O'Hara or Billy-Bob that don't only have 1 capital letter at the start of the name. If you have to handle that then you need to provide an algorithm.

这篇关于如何使用AWK从文件读取并输出到html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:13