2015-10-13 162 views
0


我有下面的純文本文件,我有一些結果,爲了郵件導致html表格格式 我已經寫了下面的腳本,它的工作很好。Unix awk模式匹配和打印行

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 

我試過模式匹配和打印線,但沒有我的想法的精選合作可能有人幫助我在此。

感謝

回答

0

我建議使用split在回答Unix: How to split a file into equal parts, without breaking individual lines?描述,然後循環使用,你已經寫的功能,如下所示:

#!/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 
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