to add special tag to a column based on column condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to add special tag to a column based on column condition
# 1  
Old 11-29-2011
to add special tag to a column based on column condition

Hi All,
I have following html code
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
Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-29-2011 at 05:56 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 11-29-2011
Try this...
Code:
awk '{match($0,"<TD>([0-9]*)</TD></TR>$",a);if(a[1]>50){gsub("<TR>","<TR style=something>")}}1' input_file

--ahamed
# 3  
Old 11-29-2011
I have following error

Code:
 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.


Last edited by Franklin52; 11-29-2011 at 05:56 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 11-29-2011
input.txt:
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>

Code:
#! /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:
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 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>


Last edited by balajesuri; 11-29-2011 at 06:15 AM..
# 5  
Old 11-29-2011
just wonder your text input.txt seems working but If I put the following listing

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>
<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

Last edited by Franklin52; 11-29-2011 at 08:34 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 11-29-2011
which is your OS? if solaris, use nawk

--ahamed
# 7  
Old 11-29-2011
@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>.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add new column based in condition

At begining of column 2 the same block (2000) have 3 lines, in the next block (2336) it have 9 lines and for block (2524) 3 lines. ... (0 Replies)
Discussion started by: jiam912
0 Replies

2. UNIX for Beginners Questions & Answers

Replacing tag based on condition

Hi All, I am having a file like below. The file will having information about the records.If you see the file the file is header and data. For example it have 1 men tag and the tag id will be come after headers. The change is I want to convert All pets tag from P to X. I did a sed like below... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

3. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

4. Shell Programming and Scripting

Check first column - average second column based on a condition

Hi, My input file Gene1 1 Gene1 2 Gene1 3 Gene1 0 Gene2 0 Gene2 0 Gene2 4 Gene2 8 Gene3 9 Gene3 9 Gene4 0 Condition: If the first column matches, then look in the second column. If there is a value of zero in the second column, then don't consider that record while averaging. ... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

5. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

6. Shell Programming and Scripting

Sum Of Column Based On Column Condition

I have a following inputfile MT,AP,CDM,TTML,MUM,GS,SUCC,3 MT,AP,CDM,TTSL,AP,GS,FAIL,9 MT,AP,CDM,RCom,MAH,GS,SUCC,3 MT,AP,CDM,RTL,HP,GS,SUCC,1 MT,AP,CDM,Uni,UPE,GS,SUCC,2 MT,AP,CDM,Uni,MUM,GS,SUCC,2 TTSL,AP,GS,MT,MAH,CDM,SUCC,20 TTML,AP,GS,MT,MAH,CDM,FAIL,10... (2 Replies)
Discussion started by: siramitsharma
2 Replies

7. Shell Programming and Scripting

Filtering lines for column elements based on corresponding counts in another column

Hi, I have a file like this ACC 2 2 21 aaa AC 443 3 22 aaa GCT 76 1 33 xxx TCG 34 2 33 aaa ACGT 33 1 22 ggg TTC 99 3 44 wee CCA 33 2 33 ggg AAC 1 3 55 ddd TTG 10 1 22 ddd TTGC 98 3 22 ddd GCT 23 1 21 sds GTC 23 4 32 sds ACGT 32 2 33 vvv CGT 11 2 33 eee CCC 87 2 44... (1 Reply)
Discussion started by: polsum
1 Replies

8. Shell Programming and Scripting

need to remove duplicates based on key in first column and pattern in last column

Given a file such as this I need to remove the duplicates. 00060011 PAUL BOWSTEIN ad_waq3_921_20100826_010517.txt 00060011 PAUL BOWSTEIN ad_waq3_921_20100827_010528.txt 0624-01 RUT CORPORATION ad_sade3_10_20100827_010528.txt 0624-01 RUT CORPORATION ... (13 Replies)
Discussion started by: script_op2a
13 Replies

9. Shell Programming and Scripting

awk to select rows based on condition on column

I have got a file like this 003ABC00281020091005000100042.810001 ... (8 Replies)
Discussion started by: Maruti
8 Replies

10. Shell Programming and Scripting

Filter the column and print the result based on condition

Hi all This is my output of the some SQL Query TABLESPACE_NAME FILE_NAME TOTALSPACE FREESPACE USEDSPACE Free ------------------------- ------------------------------------------------------- ---------- --------- ---------... (2 Replies)
Discussion started by: jhon
2 Replies
Login or Register to Ask a Question