Change the field color based on condition in email

Request your help to change the field color based on condition , if it is otherthan 0. using html in unix.
Here is my condition

for(i=1;i<=NF;i++)
{
print "<td> "$i"</td>
}

Community Rules

What you have shown us appears to be a for loop from an awk script. The only condition in this code segment is i<=NF . With this condition, it seems that you want all output to be printed, unconditionally. After all, in this code, the value of i is never zero.

Without knowing something about what your environment is and what you are trying to do, it is really hard to guess at what might solve your problem. Can you answer a few questions to help us help you?

  1. What operating system are you using?
  2. What shell are you using?
  3. What terminal type are you using?
  4. What color do you want to use?
  5. Under what condition(s) do you want to use this alternative color?
  6. What have you tried to accomplish this goal on your own?
1 Like

Hi,
In my opinion you just missed the double quote on the end.

print "<td> "$i"</td>"

Thanks for your reply, here i am attaching my desired output.

In this for the coloumn YX has value greaterthan 0 need to color with yellow

SS	GE	GE	XX	 YX	GG	       %$
AP	0	0	9	0	FALSE 	
AP	0	9	8	0	FALSE 	
AP	0	0	0	0	TRUE 	
AP	0	0	0	0	TRUE 	
CP	0	0	0	0	TRUE 	
CP	56	0	0	56	TRUE 	
CP	0	0	-9     0	FALSE

Nothing was attached to your post, and no attachment is needed. Just include the output you want in CODE tags in your post. And, if you want help, you'll help us by answering the questions I asked in post #2 in this thread.

1 Like

In addition to the other questions from post #2 that haven't been answered yet, now that we have seen post #5 please also tell us:

  1. Is a <tab> character supposed to be the field delimiter in your file?
  2. Why is there a <tab> and a <space> between the XX and YX in the header line in the first line of your output?
  3. Why are there a bunch of <space>s instead of a <tab> between the -9 and the 0 in the last line of your output?
  4. Is your input file in the same format as your output file?
  5. Will the YX field always be in the 5th field of your input file? If not how are we supposed to find it?

What operating system are you using? -LINUX
What shell are you using?- bash
What terminal type are you using?- bash
What color do you want to use? RED
Under what condition(s) do you want to use this alternative color? - Here below in coloumn YX, if any value is greaterthan 0, color to YELLOW

SS|GE|GE|XX|YX
AP|0|0|-1|0
CP|56|0|0|56
CP|0|0|-9|0

What have you tried to accomplish this goal on your own?
Data is available in mariadb, i m selecting data and writing to R.txt separated by | delimiter

I have written below normal printing as table,

<cat R.txt|awk -F'|' 'BEGIN {print "<!DOCTYPE html><html><body><table border="1" width=75% cellspacing=0> <tr><th bgcolor='"$BGC"'>.....</tr>"}{print "<tr>";
for(i=1;i<=NF;i++)
{
print "<td> "$i"</td>
} />

Is a <tab> character supposed to be the field delimiter in your file? | is the delimter
Why is there a <tab> and a <space> between the XX and YX in the header line in the first line of your output? yes this is header
Why are there a bunch of <space>s instead of a <tab> between the -9 and the 0 in the last line of your output? while copying, spaces been added.
Is your input file in the same format as your output file?input is .txt output i m expecting html and yes, the sequence is same.
Will the YX field always be in the 5th field of your input file? If not how are we supposed to find it? yes it is 5th field

Sure your code will produce a valid HTML file?

Try this modification to your sample:

awk -F'|' '
BEGIN   {print "<!DOCTYPE html><html><body><table border=\"1\" width=75% cellspacing=0><tr><th bgcolor=\"YELLOW\">.....</tr>"
         AC="style=\"color:red;\""
        }
        {print "<tr>"
         for(i=1;i<=NF;i++)     print "<td " ((i==5 && $i>0)?AC:"") "> "$i"</td>"
        print "</tr>"
        }
END     {print "</body></html>"
        }
' R.txt
1 Like

Thank you Rudic, seems it is working, but it is not working if the value is comma separated.
<Eg:2,333,555> and it is working for <5.77>

So - what would your conclusion be? What if you have 5,333,444?

Of course it isn't. You said your input file was separated by | field delimiters; not by comma delimiters. That is why RudiC gave you code that started with:

awk -F'|'

If you're going to change the field delimiter, you have to change the field delimiter in the code that will be processing your new input file format too.

This is my value in that column 5,333,444.89, such values it is treating as negative, becoz when i use <0 condition such values are all getting changed to RED and one more when i use align="right" in same condition in td it is not working.

"it is not working" doesn't really help to analyse / debug, nor do moving targets. What do you think?

If you change the script / result and end up with it not working, it's not quite fair to blame the helpers in here. How about posting the entire new script and the entire new sample data?

The commas are "thousands separators", a concept unknown to awk . Those make the field a string, which, by default, should use all leading digits up to the first non-digit when evaluating the string. BTW, the script as given works for me: print in red if given the 5,333,444.89 in field 5.

Hi Rudic, Thanks for your swift response and my code moved further after your suggestion.
It is working very perfect if the value is not comma separated.

Hi CatchMe,
I'm not sure what this means.

Are you saying your problem is resolved?

Or, are you saying that the modifications you have made to the code RudiC gave you don't work when the number given in field #5 is larger than 999? If the code doesn't work in this case are we to assume (since you haven't shown us the changes you made to the code nor supplied your new sample input format) that you do not want our help in fixing your remaining problems?

2 Likes

Hi Don and Rudic, Thanks for your super help .. yes it is working, here is how I am using to achieve my desired output and it is working irrespective of input in any format.

<col="bgcolor=RED"
{print "<tr>";
for(i=0;i<=NF;i++)
print "<td "((i==5 && $i!=0)?Col:"") " aligin='"right"'>"$i"</td>";
print "</tr>} />

Thanks again for your super support!!

Hi CatchMe,
I admit that I had trouble keeping up with your changing requirements. I was looking forward to seeing what you had come up with to solve your problem. But, now that I see your code, I'm even more confused. I have no idea what the < is doing on the first line your code.

And using the remainder of that line as a condition instead of as an assignment in a BEGIN clause means that every input line will be written out in its entirety without HTML table processing??? I don't see how that fits into any of the output formats you said you wanted to produce.

With the for loop starting with i set to zero instead of one, the entire input line will again be displayed as the first output column in your table. That clearly won't match any of the output you said you want to produce???

Am I correct in assuming that aligin was intended to be align ?

I had originally assumed that the right in this line was intended to be literal text specifying the alignment, but since the single-quotes on this line are inside double-quoted strings, the right appears to be the name of a variable that has no assigned value in the code you have shown us??? Has it been assigned a value somewhere that you haven't shown us?

The last print statement has an unterminated double-quoted string.

And, there is the extraneous /> at the end of your code fragment (or is that part of the unterminated string that is being printed)?

I'm glad that you have code that is working for you, but I don't understand how the code shown above produces anything similar to any of the output you said you want to produce.