Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

awk HTML Conditional Formating

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Dumpi16
Dumpi16's Unix or Linux Image
Dumpi16 Dumpi16 is offline
Registered User
 
Join Date: May 2018
Last Activity: 1 June 2018, 8:16 PM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
awk HTML Conditional Formating

I am receiving the below output in text format. The output is converted to HTML table using the code mentioned below

output in text


Code:
LogDate  DayOfWeek/Hours     _0_     _1_     _2_     _3_     _4_     _5_     _6_     _7_     _8_     _9_    _10_    _11_    _12_    _13_    _14_    _15_    _16_    _17_    _18_    _19_    _20_    _21_    _22_    _23_
18/05/21  Mon                85.6    51.7    28.2    33.6    28.2    30.8    24.5    20.2    53.3    96.9    80.9    92.0    44.7    84.7    57.7    65.7    23.0    31.1    20.5    17.7    20.1    14.7    46.6    67.1

Task to be achieved
I would like to highlight only these value in yellow, red or green based on condition (75 to 100 red) (40 to 74 yellow) and (0 to 39 green). I am not able to achieve the same



Code:
85.6    51.7    28.2    33.6    28.2    30.8    24.5    20.2    53.3    96.9    80.9    92.0    44.7    84.7    57.7    65.7    23.0    31.1    20.5    17.7    20.1    14.7    46.6    67.1

Can someone please help me?

Here is the code am using



Code:
awk '
        BEGIN {
                print "From: "
                print "To:"
                print "MIME-Version: 1.0"
                print "Content-Type: text/html"
                print "Subject: CPU Utilization Report"
                print "<html><body></br></br>The report provides overall Percentage Of CPU utilization Hour wise on Production system.</br></br></br>"
                print "<table border=1 cellspacing=1 cellpadding=1>"
        }
         

!/^#/ && !/^S/ {
                print "<tr>"

                for ( i = 1; i <= NF; i++ )
                {
                                             
                        
                        if ($i <=75 && $i <=100 )
                                   print "<td> <b><FONT COLOR=RED FACE="verdana"SIZE=9 ></b>"  $i  "</td>"


                        else

                        if ($i <=40 && $i <=74 )

                                   print "<td> <b><FONT COLOR=Yellow FACE="verdana"SIZE=9 ></b>"  $i  "</td>"

                        else
                                   print "<td> <FONT COLOR=GREEN  FACE="verdana"SIZE=9 >" $i "</td>"


                }
                print "</tr>"
        }
        END {
                print "</table></body></html>"
        }

'  /Reports/output/Output.txt | /usr/sbin/sendmail -t

Attached Thumbnails
awk HTML Conditional Formating-output-jpg   awk HTML Conditional Formating-text-format-output-jpg  

Last edited by Scrutinizer; 4 Weeks Ago at 02:39 PM.. Reason: adding actual output for better understanding; mod: added code tags
Sponsored Links
    #2  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Dumpi16
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 June 2018, 3:55 PM EDT
Location: Aachen, Germany
Posts: 12,919
Thanks: 429
Thanked 3,968 Times in 3,649 Posts
On first sight, and without digging deeper, shouldn't the conditions be $i >=75 and $i >=40? Be aware that there's an undiscriminated gap: e.g. 74.5 won't satisfy any condition and thus might print in green.
Sponsored Links
    #3  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Dumpi16
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 June 2018, 7:13 PM EDT
Location: San Jose, CA, USA
Posts: 11,328
Thanks: 640
Thanked 3,945 Times in 3,372 Posts
Digging just slightly deeper, note that assuming that values in your fields range from 0 through 100, the first if statement test:


Code:
 if ($i <=75 && $i <=100 )

will be satisfied only by values less than or equal to 75 (which does not seem to meet your conditions for displaying values in red. If I guessed correctly at the output you're getting, you might want to try replacing:


Code:
                        if ($i <=75 && $i <=100 )
                                   print "<td> <b><FONT COLOR=RED FACE="verdana"SIZE=9 ></b>"  $i  "</td>"


                        else

                        if ($i <=40 && $i <=74 )

                                   print "<td> <b><FONT COLOR=Yellow FACE="verdana"SIZE=9 ></b>"  $i  "</td>"

                        else
                                   print "<td> <FONT COLOR=GREEN  FACE="verdana"SIZE=9 >" $i "</td>"

with:


Code:
                        if (($i + 0) >= 75)
                                   print "<td> <b><FONT COLOR=RED FACE="verdana"SIZE=9 ></b>"  $i  "</td>"
                        else if (($i + 0) >= 40)
                                   print "<td> <b><FONT COLOR=Yellow FACE="verdana"SIZE=9 ></b>"  $i  "</td>"
                        else
                                   print "<td> <FONT COLOR=GREEN  FACE="verdana"SIZE=9 >" $i "</td>"

    #4  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Dumpi16
Dumpi16's Unix or Linux Image
Dumpi16 Dumpi16 is offline
Registered User
 
Join Date: May 2018
Last Activity: 1 June 2018, 8:16 PM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thank You

Thank you Don

The code provided is working properly and am getting the desired output.

Also thanks for explain my code if loop mistake.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk and HTML with conditional text colour jvmani_1 Shell Programming and Scripting 0 04-26-2016 07:53 AM
HTML mail formating in UNIX mohanalakshmi Shell Programming and Scripting 6 07-17-2013 02:06 AM
Formating output in html bluemind2005 Shell Programming and Scripting 5 07-04-2013 10:39 AM
formating sql file using awk or sed jville Shell Programming and Scripting 5 09-30-2010 11:38 PM
AWK: formating number without printf jolecanard Shell Programming and Scripting 5 01-15-2009 06:17 PM



All times are GMT -4. The time now is 08:21 PM.