Issue
Can someone please help me how to convert this in loop so that this logic will work for all row, I wants if column4 is greater then column3 then column4 will be in red colour same txt file I will use in email body so that highlight will be visible there
App Launch Time, Login Page, 7091, 8091
Page Load Time, Home Page, 303, 993
Page Api Response, Home Page, 861, 761
Page Parsing Time, Home Page, 141, 221
Using below shell script
a=`awk -F',' '{print $1}' 'input.csv'`
b=`awk -F',' '{print $2}' 'input.csv'`
c=`awk -F',' '{print $3}' 'input.csv'`
d=`awk -F',' '{print $4}' 'input.csv'`
if [ $d -gt $c ]
then
echo '<tr>' '<td align="center">'$a'</td>' '<td align="center">'$b'</td>' '<td align="center">'$c'</td>' '<td align="center" bgcolor="red">'$d'</td>' '</tr>' >> compare.txt
else
echo '<tr>' '<td align="center">'$a'</td>' '<td align="center">'$b'</td>' '<td align="center">'$c'</td>' '<td align="center">'$d'</td>' '</tr>' >> compare.txt
fi
echo '</table>' >> compare.txt
Output like this in txt file - Below output of 1 row require same for all row
<tr> <td align="center">AppLaunchTime</td> <td align="center">LoginPage</td> <td align="center">7091</td> <td align="center" bgcolor="red">8091</td> </tr>
Solution
awk -F', *' '
BEGIN{ print "<table>" }
{
printf "<tr>"
for (i=1; i<=NF; i++){
printf("<td align=\"center\"%s>%s</td>", (i==4 && $4>$3 ? " bgcolor=\"red\"": ""), $i)
}
print "</tr>"
}
END{ print "</table>" }
' input.csv > compare.txt
$ cat compare.txt
<table>
<tr><td align="center">App Launch Time</td><td align="center">Login Page</td><td align="center">7091</td><td align="center" bgcolor="red">8091</td></tr>
<tr><td align="center">Page Load Time</td><td align="center">Home Page</td><td align="center">303</td><td align="center" bgcolor="red">993</td></tr>
<tr><td align="center">Page Api Response</td><td align="center">Home Page</td><td align="center">861</td><td align="center">761</td></tr>
<tr><td align="center">Page Parsing Time</td><td align="center">Home Page</td><td align="center">141</td><td align="center" bgcolor="red">221</td></tr>
</table>
Answered By - ufopilot Answer Checked By - David Goodson (WPSolving Volunteer)