簡體   English   中英

在Linux中刪除包含兩種模式的行

[英]To delete lines including the two patterns in Linux

我需要刪除html文件中的某些行,例如<BR>INSTANCE NAME is : T0<BR></table>的最后一次出現之間,其中應包括上述兩種模式。

輸入樣例:

</table>
<BR>INSTANCE NAME is : T0<BR>
<table BORDER=1 CELLPADDING=2>
<TD BGCOLOR=#5D6D7E><font color=white><center>ID</center></TD> <TD BGCOLOR=#5D6D7E><font color=white><center>Find</center></TD> <TD BGCOLOR=#5D6D7E><font color=white><center>count</center></TD>
</table>
<BR>INSTANCE NAME is : T0<BR>
<table BORDER=1 CELLPADDING=2>
<TD BGCOLOR=#5D6D7E><font color=white><center>ID</center></TD> <TD BGCOLOR=#5D6D7E><font color=white><center>Find</center></TD> <TD BGCOLOR=#5D6D7E><font color=white><center>count</center></TD>
</table>
<BR>INSTANCE NAME is : T0<BR>
<table BORDER=1 CELLPADDING=2>
<TD BGCOLOR=#5D6D7E><font color=white><center>ID</center></TD> <TD BGCOLOR=#5D6D7E><font color=white><center>Find</center></TD> <TD BGCOLOR=#5D6D7E><font color=white><center>count</center></TD>
 </table>
 </BODY>
 </HTML>

預期產量:

</table>
 </BODY>
 </HTML>

我試過: sed -n '/<BR>INSTANCE NAME is : T0<BR>,</table>d/ file_name`,但是它不起作用。

任何幫助都非常歡迎!!!

sed -e '/^<BR\>/,/<\/table>/d' file_name

這將刪除包括匹配行在內的所有行,並提供所需的輸出。 需要注意的幾點:

  • 如果您只想刪除帶有特定關鍵字的某些行,則無需讓sed整個行將其刪除,僅使用關鍵字就足夠了。

  • 如果您的模式與某些字符(具有某些特殊含義的字符)匹配,則必須在關鍵字前面加上\\來轉義它們。 在這里,您需要轉義table標記的/ ,因為它具有sed特殊含義

有關sed參考,請參閱man sed

這可能對您有用(GNU sed):

sed -r '/<BR>INSTANCE NAME is : T0<BR>/,${H;$!d;x;s/.*<\/table>[^\n]*\n//}' file

<BR>INSTANCE NAME is : T0<BR>的第一個實例之間的所有行存儲<BR>INSTANCE NAME is : T0<BR>到保留空間中文件的末尾,不要通過刪除它們立即打印這些行。 在文件末尾,交換到保留空間並使用貪婪,刪除所有內容,直到</table>並打印其余部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM