【问题标题】:Unix awk pattern matching and printing linesUnix awk 模式匹配和打印行
【发布时间】:2015-10-13 23:35:14
【问题描述】:


我有以下纯文本文件,其中有一些结果,以便以 html 表格格式邮寄该结果 我已经编写了以下脚本,并且运行良好。

cat result.txt

Page  2015-01-01  2000 <br>
Colors 2015-02-01 3000 <br>
Landing 2015-03-02 4000 <br>

#!/bin/sh
LOG=/tmp/maillog.txt
RES=/tmp/result.txt

html_log () {
awk ' BEGIN { 
print "<html><body><table border=1 cellspacing=0 cellpadding=3>"
print "<tr>"
print "<td><b>Metric</b></td>";
print "<td><b>Date</b></td>";
print "<td><b>count</b></td>";
print "</tr>"
} {
print "<tr>"
print "<td>"$1"</td>";
print "<td>"$2"</td>";
print "<td>"$3"</td>";
print "</tr>"
} END {
print "</table></body></html>"
} ' $RES >> $LOG
}

#Function for sending mails
dd_mail () {
(
echo "From:xyz "
echo "To: xyz"
echo "MIME-Version: 1.0"
echo "Subject: Emp rpt" 
echo "Content-Type: text/html" 
cat $LOG
) | sendmail -t

html_log
dd_mail

exit 0

================

现在的问题是这 3 个指标很少重复(请参阅下文),如果重复,我想确定 并生成单独的 html 表,以便当我向用户发送电子邮件时,他们可以看到 2 个单独的 html 表。

cat result.txt 
Page  2015-01-01  2000
Colors 2015-02-01 3000 
Landing 2015-03-02 4000
Page  2015-01-01  1000 
Colors 2015-02-01 2000 
Landing 2015-03-02 9000

我尝试了模式匹配并打印了这些行,但我的想法都没有奏效,有人可以帮助我解决这个问题。

谢谢

【问题讨论】:

    标签: shell awk


    【解决方案1】:

    我建议按照Unix: How to split a file into equal parts, without breaking individual lines? 的回答中所述使用split,然后循环使用您已经编写的函数,如下所示:

    #!/bin/sh
    LOG=maillog.txt
    RES=result.txt
    
    html_log () {
    awk 'BEGIN { 
    print "<html><body><table border=1 cellspacing=0 cellpadding=3>"
    print "<tr>"
    print "<td><b>Metric</b></td>";
    print "<td><b>Date</b></td>";
    print "<td><b>count</b></td>";
    print "</tr>"
    } {
    print "<tr>"
    print "<td>"$1"</td>";
    print "<td>"$2"</td>";
    print "<td>"$3"</td>";
    print "</tr>"
    } END {
    print "</table></body></html>"
    } ' $RES1 >> $LOG
    }
    
    #Function for sending mails
    dd_mail () {
    (
    echo "From:xyz "
    echo "To: xyz"
    echo "MIME-Version: 1.0"
    echo "Subject: Emp rpt" 
    echo "Content-Type: text/html" 
    cat $LOG
    ) | sendmail -t
    }
    
    split -l3 $RES resChunk
    
    for RES1 in resChunk*
    do
        html_log
        dd_mail
        rm $LOG
    done
    
    exit 0
    

    【讨论】:

      【解决方案2】:

      只需少量修改即可允许多个文件(不限于 1 个。实际上每个项目出现 1 个)

      #!/bin/sh
      LOG=/tmp/maillog.txt
      Input=/tmp/result.txt
      RES=/tmp/Output
      
      html_log () {
      awk -v "Out=${RES}" ' 
         function Header( FileOut)
            { 
            print "<html><body><table border=1 cellspacing=0 cellpadding=3>" > FileOut
            print "<tr>" > FileOut
            print "<td><b>Metric</b></td>" > FileOut
            print "<td><b>Date</b></td>" > FileOut 
            print "<td><b>count</b></td>" > FileOut
            print "</tr>" > FileOut
            }
      
        function Trailer ( FileOut) {
           print "</table></body></html>" > FileOut
           }
        {
        Level = aLevel[ $1]++
        if ( Level > Highest) Highest = Level
        FileOutput = Out Level ".html"
      
        if( aFile[ Level]++ == 0 ) Header( FileOutput)
      
        print "<tr>" > FileOutput
        print "<td>"$1"</td>" > FileOutput
        print "<td>"$2"</td>" > FileOutput
        print "<td>"$3"</td>" > FileOutput
        print "</tr>" > FileOutput
        }
        END {
           for (i = 1; i <= Highest; i++) Trailer( Out i ".html")
           }
        ' ${Input}
      }
      
      #Function for sending mails
      dd_mail () {
      
      for DataFile in ${RES}*.html
       do
         (
         echo "From:xyz "
         echo "To: xyz"
         echo "MIME-Version: 1.0"
         echo "Subject: Emp rpt" 
         echo "Content-Type: text/html" 
         cat ${LOG} ${DataFile}
         ) | sendmail -t
       done
      }
      
      html_log
      dd_mail
      
      exit 0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-16
        • 1970-01-01
        • 2011-06-20
        • 2014-12-03
        • 2020-10-12
        • 2011-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多