开发者

How to use sed to insert in a line before the matching pattern

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:

  1. Match <td nowrap valign="top" and insert in the line above
  2. 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:

  1. match a line with (/\s*<tr>\s*/)
  2. continue on next line (N)
  3. 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


  1. Try grep -P "tr>\s*\n\s*<td".
  2. It's not clear how it will help you to insert something before <tr>, but anyway.
  3. Quoted strings do not nest, you need to escape the quote characters, or use single quotes instead of double quotes.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜