【问题标题】:How to post AWK output text into html tables? Only with AWK如何将 AWK 输出文本发布到 html 表中?仅使用 AWK
【发布时间】:2020-05-14 04:42:06
【问题描述】:

我是 AWK 的新手。 我试图在名为 names.txt 的文件中格式化名称,以输出以某种方式排列的名称,其中一些大写,然后将输出放入 HTML 表中。见下文。

names.txt

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

运行 AWK 后的名称输出应如下所示。

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

这是我目前在 trial.awk 文件中编写的 AWK 代码。

BEGIN{
    FS=OFS=" ";
}{

    s=tolower($0);
    split (s, letters, " ");
    array[arraylen++] = toupper( substr( letters[1], 1, 1 )  ) substr( letters[1], 2 ) " " toupper( substr( letters[2], 1, 1 )  ) substr( letters[2], 2 );
    up=toupper(substr(s,1,1));
    small=tolower(substr(s,1,1));
    as=sub(/small/, up, s);

}

END{
r=0;
for(rows=1;rows<=3;rows++){
    for(columns=r;columns<=r+6;columns+=3){
        printf("%s \t ",array[columns]);
    }r=r+1;
    columns=r;

    printf("\n");
}
}

我想如下运行它并将输出重定向到一个 HTML 文件中,每个名称都在一个单独的表格单元格中。剩下的部分是将输出放在 HTML 文件中,否则名称已经按照上面代码中的需要进行了排列和大写。请帮忙。谢谢你。让我们这样做。 awk -f trial.awk names.txt > output.html

需要的是,上面代码的输出使用 AWK 发布到 HTML 文件中,每个名称都放在单独的单元格中。 awk -f trial.awk names.txt > output.html

【问题讨论】:

    标签: shell awk


    【解决方案1】:

    你只需要打印出 HTML

    END {
        columns = 3
        print "<table>"
        for (rows = 0; rows < columns; rows++) {
            printf "<tr>"
            for (cell = rows; cell <= rows + 2*columns; cell += columns) {
                printf "<td>%s</td> ", array[cell]
            }
            print "</tr>"
        }
        print "</table>"
    }
    

    输出

    <table>
    <tr><td>Kovacs Peter</td> <td>Lisztes Tibor</td> <td>Akarki Jack</td> </tr>
    <tr><td>Kiss Roland</td> <td>Feher Andras</td> <td></td> </tr>
    <tr><td>Nagy Jolan</td> <td>Korma Maria</td> <td></td> </tr>
    </table>
    

    我想不出一个快速的方法来解释它,所以这里是答案。我使用seq 只生成 40 个项目。不幸的是,awk 没有提供ceil 函数,所以我添加了一个:

    seq 40 | awk -v columns=3 '
        function ceil(n,   i) {
            i = int(n)
            return (n > i ? i + 1 : i)
        }
        { array[arraylen++] = $1 }
        END {
            nr = ceil(arraylen / columns)
            for (row = 0; row < nr; row++) {
                for (idx = row; idx < arraylen; idx += nr) {
                    printf "%s\t", array[idx]
                }
                print ""
            }
        }
    '
    

    生产

    1   15  29
    2   16  30
    3   17  31
    4   18  32
    5   19  33
    6   20  34
    7   21  35
    8   22  36
    9   23  37
    10  24  38
    11  25  39
    12  26  40
    13  27
    14  28
    

    【讨论】:

    • 你好,你如何让它打印更多的行,从而打印更多的名字?假设我有 40 个名字。谢谢。
    • 这只是一个数学练习:让nr 为记录数,然后行数为ceil(nr / columns) -- 内部循环需要一些算术才能获得正确的索引数组。
    • 内循环算法是我试图弄清楚的。我对 AWK 很陌生。
    • 我正在尝试让它与您的代码一起使用。不成功。
    【解决方案2】:

    您能否尝试使用所示示例进行以下、编写和测试。

    awk -v total=1 '
    BEGIN{
      print "<html>" ORS "<title>" ORS "Morris Muuo answer" ORS "</title>"
      print "<head>" ORS "<style>" ORS "table, th, td {" ORS "  border: 1px solid black;" ORS "}" ORS "</style>"
      print "</style>" ORS "<body>" ORS "<table>"
    }
    {
      ++count
      a[count]=(a[count]?a[count] "\t":"")$0
    }
    FNR%3==0{
      total++
      count=""
    }
    END{
      for(i=1;i<=total;i++){
        print "<tr>"
        num=split(a[i],array,"\t")
        for(j=1;j<=num;j++){
           print "<td>" ORS array[j] ORS "</td>"
        }
        print "</tr>"
      }
      print "</table>" ORS "</body>" ORS "</html>"
    }'  Input_file
    


    说明:为上述代码添加详细说明。

    awk -v total=1 '                                                         ##Starting awk program from here, creating variable total with value 1 for it.
    BEGIN{                                                                   ##Starting BEGIN section of this code from here.
      print "<html>" ORS "<title>" ORS "Morris Muuo answer" ORS "</title>"   ##Printing starting of html, title here, please set title as per your need.
      print "<head>" ORS "<style>" ORS "table, th, td {" ORS "  border: 1px solid black;" ORS "}"  ###Printing style tag and setting CSS for table here.
      print ORS "</style>" ORS "<body>" ORS "<table>"                        ##Printing style tag completion and starting body, table tags here.
    }
    {
      ++count                                                     ##Increasing value of variable count with 1 here.
      a[count]=(a[count]?a[count] "\t":"")$0                      ##Creating array a with index of variable count and keep concatenating current line to it.
    }
    FNR%3==0{                                                     ##Checking condition if current line is fully divided by 3 here then do following.
      total++                                                     ##Increasing variable total with 1 here.
      count=""                                                    ##Nullify count here.
    }
    END{                                                          ##Starting END block for this code from here.
      for(i=1;i<=total;i++){                                      ##Starting a for loop which runs from 1 to till value of total value.
        print "<tr>"                                              ##Printing tr tag here.
        num=split(a[i],array,"\t")                                ##Splitting current value of array a value with index i into array with delimiter TAB here.
        for(j=1;j<=num;j++){                                      ##Running for loop till length of array then do following.
           print "<td>" ORS array[j] ORS "</td>"                  ##Printing td tag then value of array and closing tag td here.
        }
        print "</tr>"                                             ##Printing closing tr tag here.
      }
      print "</table>" ORS "</body>" ORS "</html>"                ##Printing all table, body and html tag here.
    }' Input_file                                                 ##Mentioning Input_file name here.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-08
      • 2011-01-28
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2017-05-31
      • 2015-05-07
      • 1970-01-01
      相关资源
      最近更新 更多