使用 awk 检查 csv 中的单元格值并在 HTML 中格式化

checking cell value in csv and formatting in HTML using awk

#!/usr/bin/awk -f      
BEGIN {            
        FS=","          
        print "<table>"          
}               
 {        
        gsub(/</, "\&lt;")        
        gsub(/>/, "\&gt;")        
        gsub(/&/, "\&gt;")        
        print "\t<tr>"        
        for(f = 1; f <= NF; f++)  {        
                if(NR == 1 && header) {        
                        printf "\t\t<th>%s</th>\n", $f        
                }         
                else printf "\t\t<td>%s</td>\n", $f        
        }               
        print "\t</tr>"        
}               

END {        
        print "</table>"        
}  

如果单元格值包含 "No",如何在循环内检查 $f 的值,然后如何使用

进行打印
printf("<TD class=AltGreen  align=right height="17" width="5%">%s</TD>\n",$f)                     
instead of  printf "\t\t<td>%s</td>\n", $f  

Input.csv

USA,NO,45    
UK,YES,90*

我在 Awk

中对您的原始逻辑进行了一些更改
  1. 在循环解析时从 $f 字段中删除空格
  2. $f 的检查包括到字符串 NO

我使用的Awk代码如下,

#!/usr/bin/awk -f
BEGIN {
        FS=","
        print "<table>"
}
 {
        gsub(/</, "\&lt;")
        gsub(/>/, "\&gt;")
        gsub(/&/, "\&gt;")
        print "\t<tr>"

        for(f = 1; f <= NF; f++)  {

            gsub(/ /, "", $f)
            if(NR == 1 && header) {
                printf "\t\t<th>%s</th>\n", $f
            }
            else if ( $f == "NO" ) {
                printf "\t\t<TD class=AltGreen  align=right height=\"17\" width=\"5%\">%s</TD>\n",$f
            }
            else printf "\t\t<td>%s</td>\n", $f

        }
        print "\t</tr>"
}

END {
        print "</table>"
}

产生的输出为

    <table>
            <tr>
                    <td>USA</td>
                    <TD class=AltGreen  align=right height="17" width="5%">NO</TD>
                    <td>45</td>
            </tr>
            <tr>
                    <td>UK</td>
                    <td>YES</td>
                    <td>90*</td>
            </tr>
    </table>

#!/usr/bin/awk -f      
BEGIN {  
   #header = 1   
   # for the no in OP and NO in sample       
   IGNORECASE = 1

   FS=","          
   print "<table>"          
   }               
   {        
   gsub(/</, "\&lt;")        
   gsub(/>/, "\&gt;")        
   gsub(/&/, "\&gt;")        
   print "\t<tr>"        
   for(f = 1; f <= NF; f++)  {        
      if(NR == 1 && header) {        
         printf "\t\t<th>%s</th>\n", $f        
         }         
         else {
            # your NO filtering
            if ( $f ~ /^NO$/) {
               printf("<TD class=AltGreen  align=right height=\"17\" width=\"5%\">%s</TD>\n", $f)
             else {
               printf "\t\t<td>%s</td>\n", $f
               }
            }
        }               
        print "\t</tr>"        
   }               

END {        
   print "</table>"        
   }  
  • 我只是稍微修改了一下你的代码,让它尽可能地保持你所做的。
  • 使用$f ~ //
  • 我添加了 IGNORECASE,0 区分大小写,1 不区分大小写
  • 为 HTML 输出的引用值调整双引号

一些说明: 我想你想用 gsub(/&/, "\&amp;").
替换 gsub(/&/, "\&gt;") 当您勾选 NR 时,您不需要 header。 当你也想检查 header 中的 "NO" 时,你可以执行类似

的操作
echo "USA,NO,45
UK,YES,90*" | awk '
BEGIN {
   FS=","
   print "<table>"
 }
 {
    gsub(/</, "\&lt;")
    gsub(/>/, "\&gt;")
    gsub(/&/, "\&amp;")
    print "\t<tr>"
    if(NR==1) {
        tag="th"
    } else {
       tag="td"
    }
    for (f = 1; f <= NF; f++)  {

        if ( $f =="NO") {
           printf("<%s class=AltGreen align=right height=\"17\" width=\"5%%\">%s</%s>\n",
               tag, $f, tag)   
        } else {
           printf "\t\t<%s>%s</%s>\n", tag, $f, tag
        }
     }
     print "\t</tr>"
  }