Reputation: 4427
I am dealing with some html code and i got stucked in some problem. Here is the extract of some code and the format is exactly the same
<tr>
<td nowrap valign="top" class="table_1row"><a name="d071301" id="d071301"></a>13-Jul-2011</td>
<td width="21%" valign="top" class="table_1row"><a href="http://www.info.htm" target="_blank">LCQ8: Personal data of job</a></td>
Here i have to match with
<tr>
<td nowrap valign="top"
and insert something before <tr>
.the problem occurs as i have to match a pattern in different lines.
i have tried
grep -c "<tr>\n<td nowrap valign="top"" test.html
grep -c "<tr>\n*<td nowrap valign="top"" test.html
grep -c "<tr>*<td nowrap valign="top"" test.html
to test but none of them works.So i have two dimension to figure out the problem:
- Match
<td nowrap valign="top" and insert in the line above
- Match whole string
<tr>
<td nowrap valign="top"
Would anyone suggest a way to doing it in either way?
Upvotes: 0
Views: 1855
Reputation: 9474
Using sed you can perfom replacement on multiple lines. Its also easy to substitute the match.
sed "/\s*<tr>\s*/ { N; s/.*<tr>\n\s*<td.*/insertion\n&/ }"
This cryptic line basically say:
/\s*<tr>\s*/
)N
)s/.*<tr>\n\s*td.*/insertion\n&/
)Sed is very powerful to perform substitution, its a nice to know tool. See this manual if you want to learn more about sed: http://www.grymoire.com/Unix/Sed.html
Upvotes: 2
Reputation: 119847
grep -P "tr>\s*\n\s*<td"
.<tr>
, but anyway.Upvotes: 0