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 anyon开发者_StackOverflow中文版e suggest a way to doing it in either way?
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:
- match a line with (
/\s*<tr>\s*/
) - continue on next line (
N
) - substitute the matched pattern whit the insertion and the matched string, where & represent the matched string (
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
- Try
grep -P "tr>\s*\n\s*<td"
. - It's not clear how it will help you to insert something before
<tr>
, but anyway. - Quoted strings do not nest, you need to escape the quote characters, or use single quotes instead of double quotes.
精彩评论