HTML color code and tabluar issue

input data in a file

servic webservice.somthing 200 OK
servic1 webservice.somthing 200 OK
servic1 webservice.somthing 400 BAD REQEST

Below script is making tabular form perfectly. but there are two thing i am not able to achive
1.how can i color the complete row as red when it see '400' in the row, eg line 3rd from the file.
2 BAD REQUEST have space in between, and because of that BAD is coming in one column and REQUEST is coming in another, how can i get this BAD REQUEST in one column

echo "<br />"  > output.html
echo "<html>" >> output.html
echo "<Body>" >> output.html
nawk 'BEGIN{print "<table border="1">"} 
          {print "<tr>";
                  for(i=1;i<=NF;i++)print "<td>" $i"</td>";
                  print "</tr>"} END{print "</table>"}' output.txt  >> output.html
echo "</Body>" >> output.html
echo "</html>" >> output.html
echo "<br />"  >> output.html
  1. Is it always the same field that needs to be checked (here: field 3)? Then you might try
awk '
BEGIN   {print "<br />\n<html>\n<Body>\n<table border=\"1\">"
        }
        {if ($3+0 >= 400) print "<tr style=\"color:red\">"
         else print "<tr>"
         for(i=1;i<=NF;i++)print "<td>" $i "</td>";
         print "</style /tr>"
        }
END     {print "</table>\n</Body>\n</html>\n<br />"
        }
' file >output.html
  1. If the line's field separator is the same as the space between the two words, you're out of luck - there's no means to tell between the two. Make sure the producing command/programme/application uses a diferent FS, e.g. a <TAB> char.
    Unless the field count is known and fix - then you could count the fields and add any surplus to the status field.
1 Like

Yes it is the same field that need to be checked. i mean all the lines should come in out put, but the line have 400 in it should be highlighted in red

and wont we have any alternative to fix the space issue.
like if i capture all the four parameters in variable and then can i pass all those variable in such HTML code to achive it?

How do you want to make ANYTHING that reads those lines, be it (shell) read , awk , any text utility, ..., tell the difference between the last and the before last space?

Dear Rudic,
I am not sure how we can do it, but using sed can I omit this space between BAD and REQUEST .
then i will omit the space and take the data in other file and run HTML script on that

If you say there's ONLY those two words possible - NO OTHER multi word status could come up - then of course you can do it in any tool, sed as well as awk as well as ...
Try sub (/BAD REQUEST/, "BAD_REQUEST") in above proposal.

What if e.g. FAILURE IN QUERY would be produced?

The data in file is limited, either it is ok or bad request.
and how can I use sub (/BAD REQUEST/, "BAD_REQUEST")
you mean I have to use it in HTML code or separately on the file.

Put it just in front of the if ($3+0 >= 400) .

1 Like

Rudic, it worked perfect but color code as RED is not working, it is coming normal

Try style="color:#FF0000"

still same issue.
see if i am doing anything wrong

nawk '
BEGIN   {print "<br />\n<html>\n<Body>\n<table border=\"1\">"
        }
        {if ($3+0 >= 400 sub (/BAD REQUEST/, "BAD_REQUEST")) print "<tr style=\"color:#FF0000\">"
         else print "<tr>"
         for(i=1;i<=NF;i++)print "<td>" $i "</td>";
         print "</style /tr>"
        }
END     {print "</table>\n</Body>\n</html>\n<br />"
        }
' output.txt >output.html

"in front of" means IN FRONT OF! Not AFTER

{sub (/BAD REQUEST/, "BAD_REQUEST")
 if ($3+0 >= 400) print "<tr style=\"color:#FF0000\">"
 else print "<tr>"
1 Like

I am sorry for the confusion,
now I am getting below error

nawk: syntax error at source line 4
 context is
                {sub (/BAD REQUEST/, "BAD_REQUEST") >>>  if <<<  ($3+0 >= 400)) print "<tr style=\"color:#FF0000\">"
nawk: illegal statement at source line 4
nawk: extra ) at source line 5
        extra )

---------- Post updated at 11:05 PM ---------- Previous update was at 11:01 PM ----------

I got it now dear. Thanks a TON