</tr>
<tr class='htmllist_tr' style="background-color:yellow" ><td class='htmllist_td' >INDX01</td>
<td class='htmllist_td_nbr' >964.87</td>
<td class='htmllist_td_nbr' >95.13</td>
<td class='htmllist_td' >NehaA9.86</td>
</tr>
<tr class='htmllist_tr' ><td class='htmllist_td' >UNDOTBS1</td>
<td class='htmllist_td_nbr' >156.25</td>
<td class='htmllist_td_nbr' >8</td>
<td class='htmllist_td' >NehaA5.12</td>
</tr>
想要在<tr>
和</tr>
代码之间找到NehaA,然后更改
`<tr class='htmllist_tr'>`
或
<tr class='htmllist_tr' style="background-color:yellow">
到
`<tr class='htmllist_tr' style="background-color:red">` *
尝试了这个
sed -e "/NehaA/ s/\'<tr class='htmllist_tr'>\'/\'<tr class='htmllist_tr' style="background-color:red">\'/ ;" 2932_TABLE2.txt
没有工作请帮助
答案 0 :(得分:1)
如果您没有使用HTML解析器获得可用的答案,请尝试以下操作:
$ awk -v RS='</tr>\\s*' '/Neha/{ORS=RT; sub(/<tr[^>]+>/,""); print "<tr class=\047htmllist_tr\047 style=\"background-color:red\">" $0}' file
<tr class='htmllist_tr' style="background-color:red"><td class='htmllist_td' >INDX01</td>
<td class='htmllist_td_nbr' >964.87</td>
<td class='htmllist_td_nbr' >95.13</td>
<td class='htmllist_td' >NehaA9.86</td>
</tr>
<tr class='htmllist_tr' style="background-color:red"><td class='htmllist_td' >UNDOTBS1</td>
<td class='htmllist_td_nbr' >156.25</td>
<td class='htmllist_td_nbr' >8</td>
<td class='htmllist_td' >NehaA5.12</td>
</tr>
它使用GNU awk进行多字符RS和RT。
答案 1 :(得分:0)
这是我使用HTML::TreeBuilder
的方法。代码本身就是自我放纵的。我建议您阅读文档,因为不建议您解析HTML using regex
。
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TreeBuilder;
my $str = <<'HTML'
<html>
<head>
</head>
<body>
<table>
<tr class='htmllist_tr' style="background-color:yellow" >
<td class='htmllist_td' >INDX01</td>
<td class='htmllist_td_nbr' >964.87</td>
<td class='htmllist_td_nbr' >95.13</td>
<td class='htmllist_td' >NehaA9.86</td>
</tr>
<tr class='htmllist_tr' >
<td class='htmllist_td' >UNDOTBS1</td>
<td class='htmllist_td_nbr' >156.25</td>
<td class='htmllist_td_nbr' >8</td>
<td class='htmllist_td' >NehaA5.12</td>
</tr>
</table>
</body>
</html>
HTML
;
my $root = HTML::TreeBuilder->new_from_content($str);
my @tr = $root -> find_by_tag_name('tr');
foreach (@tr) {
if ($_ -> find_by_attribute("class","htmllist_tr")) {
my @tds = $_ -> look_down(_tag => 'td', class => 'htmllist_td');
my @children = map {$_ -> content_list} @tds;
if(grep(/NehaA/, @children)) {
$_ -> attr('style', 'background-color:red');
}
}
}
print $root -> as_HTML(undef, " ");
答案 2 :(得分:0)
@ED ..来自cofusion ..这是原始文件
<table class='htmllist'>
<tr class='htmllist_tr' ><th class='htmllist_th' >TABLESPACE<br>NAME</th>
<th class='htmllist_th' >ALLOCATED<br>SPACE<br>GB</th>
<th class='htmllist_th' >CURRENT<br>FREE<br>SPACE<br>GB</th>
<th class='htmllist_th' >CURRENT<br>FREE<br>SPACE<br>PCT</th>
<tr class='htmllist_tr' style="background-color:yellow" ><td class='htmllist_td' >INDX01</td>
<td class='htmllist_td_nbr' >964.87</td>
<td class='htmllist_td_nbr' >95.78</td>
<td class='htmllist_td' >NehaA9.93</td>
</tr>
<tr class='htmllist_tr' ><td class='htmllist_td' >TEMP</td>
<td class='htmllist_td_nbr' >125</td>
<td class='htmllist_td_nbr' >124.63</td>
<td class='htmllist_td_nbr' >99.7</td>
</tr>
<tr class='htmllist_tr' ><td class='htmllist_td' >TEMP_EDDDATA</td>
<td class='htmllist_td_nbr' >205.99</td>
<td class='htmllist_td_nbr' >198.52</td>
<td class='htmllist_td_nbr' >96.37</td>
</tr>
<tr class='htmllist_tr' ><td class='htmllist_td' >UNDOTBS1</td>
<td class='htmllist_td_nbr' >156.25</td>
<td class='htmllist_td_nbr' >22.85</td>
<td class='htmllist_td' >NehaA14.62</td>
</tr>
</table>
我希望输出
<table class='htmllist'>
<tr class='htmllist_tr' ><th class='htmllist_th' >TABLESPACE<br>NAME</th>
<th class='htmllist_th' >ALLOCATED<br>SPACE<br>GB</th>
<th class='htmllist_th' >CURRENT<br>FREE<br>SPACE<br>GB</th>
<th class='htmllist_th' >CURRENT<br>FREE<br>SPACE<br>PCT</th>
<tr class='htmllist_tr' style="background-color:red" ><td class='htmllist_td' >INDX01</td>
<td class='htmllist_td_nbr' >964.87</td>
<td class='htmllist_td_nbr' >95.78</td>
<td class='htmllist_td' >NehaA9.93</td>
</tr>
<tr class='htmllist_tr' ><td class='htmllist_td' >TEMP</td>
<td class='htmllist_td_nbr' >125</td>
<td class='htmllist_td_nbr' >124.63</td>
<td class='htmllist_td_nbr' >99.7</td>
</tr>
<tr class='htmllist_tr' ><td class='htmllist_td' >TEMP_EDDDATA</td>
<td class='htmllist_td_nbr' >205.99</td>
<td class='htmllist_td_nbr' >198.52</td>
<td class='htmllist_td_nbr' >96.37</td>
</tr>
<tr class='htmllist_tr' style="background-color:red"><td class='htmllist_td' >UNDOTBS1</td>
<td class='htmllist_td_nbr' >156.25</td>
<td class='htmllist_td_nbr' >22.85</td>
<td class='htmllist_td' >NehaA14.62</td>
</tr>
</table>
然而,当我使用这个
时awk -v RS='</tr>\\s*' '/Neha/{ORS=RT; sub(/<tr[^>]+>/,""); print "<tr class=\047htmllist_tr\047 style=\"background-color:red\">" $0}' text.txt
这给了我这样的输出
<tr class='htmllist_tr' style="background-color:red"><table class='htmllist'>
<th class='htmllist_th' >TABLESPACE<br>NAME</th>
<th class='htmllist_th' >ALLOCATED<br>SPACE<br>GB</th>
<th class='htmllist_th' >CURRENT<br>FREE<br>SPACE<br>GB</th>
<th class='htmllist_th' >CURRENT<br>FREE<br>SPACE<br>PCT</th>
<tr class='htmllist_tr' style="background-color:yellow" ><td class='htmllist_td' >INDX01</td>
<td class='htmllist_td_nbr' >964.87</td>
<td class='htmllist_td_nbr' >95.78</td>
<td class='htmllist_td' >NehaA9.93</td>
</tr><tr class='htmllist_tr' style="background-color:red">
<td class='htmllist_td' >UNDOTBS1</td>
<td class='htmllist_td_nbr' >156.25</td>
<td class='htmllist_td_nbr' >22.85</td>
<td class='htmllist_td' >NehaA14.62</td>
</tr>
让我知道这是否有意义