to add special tag to a column based on column condition

Hi All,
I have following html code

<TR><TD>9</TD><TD>AR_TVR_TBS          </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR>

<TR><TD>10</TD><TD>ASCV_SMY_TBS        </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR>

<TR><TD>11</TD><TD>ARC_TBS             </TD><TD>1662</TD><TD>848</TD><TD>1598</TD><TD>51</TD></TR>

<TR><TD>12</TD><TD>IDX_TBS             </TD><TD>45120</TD><TD>20526</TD><TD>38492</TD><TD>45</TD></TR>

If you able to see above list properly, I had put the last column to red color
whereby to show you that that is the column that I want to put it as condition ie. if it is more than 50, I will need to substitute <TR> into <TR style...>, and remaining rows I still want to display out to show that that particular rows has exceeded certain percentage.

Thanks

Please use code tags for code and data samples, thank you

Try this...

awk '{match($0,"<TD>([0-9]*)</TD></TR>$",a);if(a[1]>50){gsub("<TR>","<TR style=something>")}}1' input_file

--ahamed

I have following error

 Syntax Error The source line is 1.
 The error context is
                 >>> {match($0,"<TD>([0-9]*)</TD></TR>$", <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

input.txt:

<TR><TD>9</TD><TD>AR_TVR_TBS </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR>
<TR><TD>10</TD><TD>ASCV_SMY_TBS </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR>
<TR><TD>11</TD><TD>ARC_TBS </TD><TD>1662</TD><TD>848</TD><TD>1598</TD><TD>51</TD></TR>
<TR><TD>12</TD><TD>IDX_TBS </TD><TD>45120</TD><TD>20526</TD><TD>38492</TD><TD>45</TD></TR>
#! /usr/bin/perl -w
use strict;
open INPUT, "< input.txt";
open OUTPUT, ">> output.txt";
for my $x ( <INPUT> ) {
    if ($x =~ /<TD>5[0-9]+<\/TD><\/TR>$/) {
        $x =~ s/TR>/TR style>/g;
        print OUTPUT $x;
    }
    else {
        print OUTPUT $x;
    }
}
close OUTPUT;
close INPUT;

output.txt:

<TR><TD>9</TD><TD>AR_TVR_TBS </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR>
<TR><TD>10</TD><TD>ASCV_SMY_TBS </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR>
<TR style><TD>11</TD><TD>ARC_TBS </TD><TD>1662</TD><TD>848</TD><TD>1598</TD><TD>51</TD></TR style>
<TR><TD>12</TD><TD>IDX_TBS </TD><TD>45120</TD><TD>20526</TD><TD>38492</TD><TD>45</TD></TR>

just wonder your text input.txt seems working but If I put the following listing

<TR><TD>9</TD><TD>AR_TVR_TBS          </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR>
<TR><TD>10</TD><TD>ASCV_SMY_TBS        </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR>
<TR><TD>11</TD><TD>ARC_TBS             </TD><TD>1662</TD><TD>848</TD><TD>1598</TD><TD>51</TD></TR>
<TR><TD>12</TD><TD>IDX_TBS             </TD><TD>45120</TD><TD>20526</TD><TD>38492</TD><TD>45</TD></TR>
<TR><TD>14</TD><TD>AR_TVR_LARGE_1_TBS  </TD><TD>72000</TD><TD>30024</TD><TD>57544</TD><TD>41</TD></TR>
<TR><TD>15</TD><TD>AR_TVR_LARGE_2_TBS  </TD><TD>72000</TD><TD>17658</TD><TD>49156</TD><TD>24</TD></TR>
<TR><TD>9</TD><TD>AR_TVR_TBS          </TD><TD>85000</TD><TD>40104</TD><TD>54458</TD><TD>47</TD></TR>
<TR><TD>10</TD><TD>ASCV_SMY_TBS        </TD><TD>69880</TD><TD>36068</TD><TD>48696</TD><TD>51</TD></TR>
<TR><TD>11</TD><TD>ARC_TBS             </TD><TD>1662</TD><TD>856</TD><TD>924</TD><TD>51</TD></TR>
<TR><TD>12</TD><TD>IDX_TBS             </TD><TD>45120</TD><TD>19322</TD><TD>37316</TD><TD>42</TD></TR>

it will not work anymore.

Thanks

which is your OS? if solaris, use nawk

--ahamed

@ckwan: Why will it not work? Did you try with the second set of input? Script worked perfectly for your second set of input too. Lines with last <TD> column > 50 had their <TR> tags replaced with <TR style>.

Try this...

awk -F"<TD>" '{a=$NF;gsub(/[^0-9]/,"",a);if(a>50){gsub("<TR>","<TR style=something>")}}1' input_file

or

awk -F"<TD>" '{a=$NF;if(a+0>50){gsub("<TR>","<TR style=something>")}}1' input_file

--ahamed

Hi Ahamed, I am using AIX.

Hi balajesuri, I read your perl script code that seems it only match by pattern (if I read correctly, sorry I am new to perl script),
where I need the number comparison if ($no > 50), I only want to add <TR style...>, if less than 50 will not add "style" into <TR>. This is normally happen when admin person would like to highlight those > x%, the script will highlight those rows hit the threshold.

Similarly to Ahamed, the script is matching by pattern. it will be good if the script can cater for the above scenario.

Thanks for your afford.

---------- Post updated at 10:33 PM ---------- Previous update was at 09:20 PM ----------

Ahamed, thanks for your code. Is working now.

Thanks to balajesuri too.. I believe perl should able to perform the same task too. It will be good if you can send me the code as well. I am also keen on perl scripting.

Thanks all guys.

Sorry. My mistake. Try this regular expression to match any number strictly > 50:

/<TD>(5[1-9]|[6-9][0-9]|[1-9][0-9]{2,})<\/TD><\/TR>$/