Unix awk模式匹配和打印行

时间:2015-10-13 23:35:15

标签: shell awk


  我有以下纯文本文件,我有一些结果,以便以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

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

现在问题是这三个指标很少重复(参见下文),如果它重复我想要识别 并生成单独的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

我尝试过模式匹配并打印出这些线条,但我的想法都没有用,有人可以帮我解决这个问题。

由于

2 个答案:

答案 0 :(得分:0)

我建议在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

答案 1 :(得分:0)

很少修改以允许多个文件(不限于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