Help with awk color codes based on condition

HI

i have two files say test and test1

Test.txt

Code:
Lun01	2TB	1.99TB	99.6%
Lun02	2TB	1.99TB	99.5%
Lun03	2TB	1.99TB	99.5%
Lun04        2TB   1.55TB       89.6% 

Code:
Test1.txt

Lun01	2TB	1.99TB	89.5%
Lun02	2TB	1.99TB	99.5%
Lun03	2TB	1.99TB	99.5%

Requirement is to compare Test.txt and Test1.txt and highlight the content to red color in Test.txt which is not there in Test.txt

Expected output.
Code:

Lun01	2TB	1.99TB	99.6%
Lun04        2TB   1.55TB       89.6%
Lun02	2TB	1.99TB	99.5%
Lun03	2TB	1.99TB	99.5%

I guess you meanin Text1.txt.

Why reordering the lines in your expected output?

hi

sorry its test1.txt.

reordering is not required.

just highlighting of red color is enough

Hello venkitesh,

Could you please try following and let me know if this helps you.

awk 'FNR==NR{A[$1,$NF]=$0;next} (($1,$NF) in A){print} !(($1,$NF) in A){print "\033[31m"$0"\033[0m";}' test1.txt test.txt

Thanks,
R. Singh

1 Like

Hi Ravinder,

works like a charm in the terminal.

But i am converting this in html and sending via sendmail

this is what i am getting in outlook.

[31mLun01	      2TB	1.99TB	99.6% [0m 
[31mLun04              2TB     1.55TB       89.6% [0m

plz find my bash script

awk 'FNR==NR{A[$1,$NF]=$0;next} (($1,$NF) in A){print} !(($1,$NF) in A){print "\033[31m"$0"\033[0m";}' test1 test > finaloutput

awk ' BEGIN {
 print "From:test@test.com"
 print "To:test@test.com"
 print "Subject: CRITICAL"
 print "MIME-Version: 1.0"
 print "Content-Type: text/html"
 print "<body bgcolor=\"#ffffff\" text=\"#000000\">"
 print "<html><body><table border=1 cellspacing=0 cellpadding=3>"
 print "<tr>"
 print "<td>LUN NAME</td>";
 print "<td>TOTAL SPACE</td>";
 print "<td>USED SPACE</td>";
 print "<td>USED %</td>";
 print "</tr>"
} {
 print "<tr>"
 print "<td><font face=verdana size=2 color=green>"$1"</td>";
 print "<td><font face=verdana size=2 color=green>"$2"</td>";
 print "<td><font face=verdana size=2 color=green>"$3"</td>";
 print "<td><font face=verdana size=2 color=green>"$4"</td>";
 print "</tr>"
} END {
 print "</table></body></html>"
} ' finaloutput | sendmail -t

Color code needs to be added to your generated HTML as outlook doesn't support ANSI escape sequences try:

awk '
BEGIN {
 print "From:test@test.com"
 print "To:test@test.com"
 print "Subject: CRITICAL"
 print "MIME-Version: 1.0"
 print "Content-Type: text/html"
 print "<body bgcolor=\"#ffffff\" text=\"#000000\">"
 print "<html><body><table border=1 cellspacing=0 cellpadding=3>"
 print "<tr>"
 print "<td>LUN NAME</td>";
 print "<td>TOTAL SPACE</td>";
 print "<td>USED SPACE</td>";
 print "<td>USED %</td>";
 print "</tr>"
}
FNR==NR {A[$0];next}
{
 if($0 in A) C="green"
 else C="red"
 print "<tr>"
 for(i=1;i<=4;i++) print "<td><font face=verdana size=2 color=" C ">" $i "</td>";
 print "</tr>"
}
END { print "</table></body></html>"} ' test1.txt test.txt | sendmail -t
1 Like

hi all

worked perfectly

thanks