LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-20-2020, 11:51 AM   #1
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Rep: Reputation: Disabled
Displaying the cell in different color


Hi Team,

My requirement is to display the value of html cell in red color based on the value in another cell .
Example:The value in the column "Actual count" should be displayed in red color as it is greater than value in "threshold" column.
The content of testresult.txt are as follows
Properties 20201020 281899 32000 Warning

Code:
fn_presentationLogic()
{

awk 'BEGIN{
FS=" "
print  "<HTML>""<TABLE border="1"><TH>Item</TH><TH>Date</TH><TH>Actual count</TH><TH>Threshold</TH><TH>Status</TH> "
}
 {
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
 }
END{
print "</TABLE></BODY></HTML>"
 }
'  testresult.txt > HotelDashboard.html

sed -i "s/Warning/<font color="red">$3<\/font>/g;s/Normal/<font color="green">$3<\/font>/g" HotelDashboard.html
}
fn_presentationLogic
I have used the above sed command to substitute the third cell if it finds "Warning" but in vain. Could you please advise.
 
Old 10-20-2020, 02:26 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by nextStep View Post
The content of testresult.txt are as follows
Properties 20201020 281899 32000 Warning

Code:
...
sed -i "s/Warning/<font color="red">$3<\/font>/g;s/Normal/<font color="green">$3<\/font>/g" HotelDashboard.html
}
fn_presentationLogic
I have used the above sed command to substitute the third cell if it finds "Warning" but in vain. Could you please advise.
There are several problems.
Problem 1: sed doesn't have the notion of columns. For sed, $3 is just the string "$3", not the third column.
Problem 2: You don't quote correctly. The first double quote is matched with the double quote before red. You then have an output redirection, $3, and input redirection plus more stuff. The result is probably an incorrect shell command, and definitely not a working sed command. You could fix that by replacing the very first and very last double quotes with single quotes, but unfortunately, problem 1 will subsist.

Since sed doesn't know anything about columns, it is not the right tool for the task. awk, on the other hand, is well-suited. I would add the logic of colouring column 3 to the awk command that you have written already. First though, make sure that the current awk command generates the expected output.

Last edited by berndbausch; 10-20-2020 at 02:29 PM.
 
1 members found this post helpful.
Old 10-21-2020, 12:22 AM   #3
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks Berndbausch.The current awk command is working fine , the html file is generating fine.Below is the output from awk command.
Item Date Actual count Threshold Status
Properties 20201020 281899 32000

Could you please shed some light on the awk logic for displaying the cell color as i was unable to find anything from my research on the various forums.
Thanks in advance.
 
Old 10-21-2020, 12:44 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by nextStep View Post
Thanks Berndbausch.The current awk command is working fine , the html file is generating fine.Below is the output from awk command.
Item Date Actual count Threshold Status
Properties 20201020 281899 32000

Could you please shed some light on the awk logic for displaying the cell color as i was unable to find anything from my research on the various forums.
Thanks in advance.
You say "The value in the column "Actual count" should be displayed in red color as it is greater than value in "threshold" column.".

This can be achieved by replacing the second action in your awk program with
Code:
{ 
    printf "<TR>"
    for(i=1;i<=NF;i++) { 
        if (i==2 && $2>$3)
           printf "<TD><font color=\"red\">%s</font></TD>", $i
        else
           printf "<TD>%s</TD>", $i
    }
    print "</TR>" 
}
I have not tested it. The idea is to check the condition actual count greater than threshold inside the for loop.

Last edited by berndbausch; 10-21-2020 at 12:46 AM. Reason: formatting
 
1 members found this post helpful.
Old 10-21-2020, 01:30 AM   #5
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks a lot Berndbausch. This has resolved my issue. Much appreciated.
 
  


Reply

Tags
awk, html, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
why did I lose my table cell lines format when I changed the cell 1sweetwater! Linux - Software 1 12-03-2014 04:06 PM
Why does open office put lines into adjacent cell when I am splitting one cell 1sweetwater! Linux - Software 1 12-03-2014 01:36 PM
LibreOffice Writer: How to color single cell with multiple colors ghantauke Linux - General 1 09-21-2012 08:56 PM
odbc (php output not displaying in web browser,but it displaying in terminal) manu@vssc Red Hat 1 06-29-2011 01:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration