与えられた列から与えられた文字列だけを削除しますか?

与えられた列から与えられた文字列だけを削除しますか?

入力する:

<tr><td>FOOBAAR</td><td>FOOO</td><td>BAAR</td><td><font style=BACKGROUND-COLOR:red>2014-02-14 13:34</font></td><td><font style=BACKGROUND-COLOR:red>2014-02-17 13:34</font></td><td><font style=BACKGROUND-COLOR:red>2014-03-07 13:34</font></td></tr>

出力:

<tr><td>FOOBAAR</td><td>FOOO</td><td>BAAR</td><td>2014-02-14 13:34</td><td><font style=BACKGROUND-COLOR:red>2014-02-17 13:34</font></td><td><font style=BACKGROUND-COLOR:red>2014-03-07 13:34</font></td></tr>

違い:

<font style=BACKGROUND-COLOR:red>

そして

</font>

4番目の列からのみ削除します。

私の質問:与えられた列から与えられた文字列だけを削除するには?

</td><td>

区切り記号

ベストアンサー1

正規表現を使用するよりもHTML解析ツールを使用する方が良いです。 (有名な答えが理由を説明します。ここ)

以下はXMLパーサーを使用する例です(注:入力は正しい形式のXMLでなければなりませんが、サンプルHTMLはそうではありません)。

# change the value of the style attribute of the font tag of the 4th td tag 
# to the empty string
xmlstarlet ed -O -u '//table/tr/td[4]/font[@style]/@style' -v "" <<END
<html><head></head><body><table>
<tr><td>FOOBAAR</td><td>FOOO</td><td>BAAR</td><td><font style="BACKGROUND-COLOR:red">2014-02-14 13:34</font></td><td><font style="BACKGROUND-COLOR:red">2014-02-17 13:34</font></td><td><font style="BACKGROUND-COLOR:red">2014-03-07 13:34</font></td></tr>
</table></body></html>
END
<html>
  <head/>
  <body>
    <table>
      <tr>
        <td>FOOBAAR</td>
        <td>FOOO</td>
        <td>BAAR</td>
        <td>
          <font style="">2014-02-14 13:34</font>
        </td>
        <td>
          <font style="BACKGROUND-COLOR:red">2014-02-17 13:34</font>
        </td>
        <td>
          <font style="BACKGROUND-COLOR:red">2014-03-07 13:34</font>
        </td>
      </tr>
    </table>
  </body>
</html>

おすすめ記事