0


I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well.

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

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

Now the problem is very rarely these 3 metrics are repeating (refer below), if it repeats i want to identify and generate the separate html table so that when i send email to users they can see 2 separate html tables.

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

I tried pattern matching and print the lines but none of my idea's worked could someone help me on this.

Thanks

Simon
  • 10,679
  • 1
  • 30
  • 44
Jay
  • 23
  • 4

2 Answers2

0

I suggest using split as described in an answer to Unix: How to split a file into equal parts, without breaking individual lines? and then looping to use the functions that you've already written, as follows:

#!/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
Community
  • 1
  • 1
Simon
  • 10,679
  • 1
  • 30
  • 44
0

With few modification to allow multiple file (not limited to 1. In fact 1 per item occurrence)

#!/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
NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43