2011-07-19 90 views
0

我正在处理一些html代码,并且遇到了一些问题。下面是一些代码提取物和格式完全相同如何在匹配模式之前使用sed插入一行

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

在这里,我不得不配合

            <tr> 
         <td nowrap valign="top" 

<tr>发生。问题前插入的东西,因为我有匹配模式在不同的路线。 我试图

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 

测试,但他们都不works.So我有两个维度,找出问题:

  1. 比赛<td nowrap valign="top" and insert in the line above
  2. 匹配整个字符串
           <tr> 
        <td nowrap valign="top" 

有人会提出一种方法来做到这一点吗?

+0

[希望这类似的问题,我之前提出可以帮你] [1] [1]:http://stackoverflow.com/如果您想了解更多的sed请参见本手册question/6624199/how-can-i-append-a-string-into-the-first-occurrence of-a-pattern –

+1

also http://stackoverflow.com/q/6732702/7552 –

回答

0
  1. 尝试grep -P "tr>\s*\n\s*<td"
  2. 目前尚不清楚它将如何帮助您在<tr>之前插入某些东西,但无论如何。
  3. 引用的字符串不嵌套,您需要转义引号字符,或使用单引号而不是双引号。
+0

no it can not搜索任何东西 –

2

使用sed,您可以在多行上执行更换。它也很容易替代比赛。

sed "/\s*<tr>\s*/ { N; s/.*<tr>\n\s*<td.*/insertion\n&/ }" 

此隐蔽线基本上说:

  1. 匹配带(/\s*<tr>\s*/
  2. 线继续下一行(N
  3. 替代匹配模式白衣插入和匹配的字符串,其中&表示匹配的字符串(s/.*<tr>\n\s*td.*/insertion\n&/

Sed是非常强大的执行替代,它是一个很好的知道工具。 http://www.grymoire.com/Unix/Sed.html

相关问题