簡體   English   中英

正則表達式排除字符串並刪除行

[英]regex to exclude string and delete line

我在 XML 文件中有以下幾行

<User id="10338" directoryId="1" sometext txt text test/>
<User id="10359" directoryId="100" some more text text text/>
<User id="103599" directoryId="100" some more text text text/>
<User id="10438" directoryId="1" sometext txt text test/>

我正在嘗試刪除以 User id=" 開頭的所有行,但我想保留那些具有 directoryId="1" 的行

我當前的 sed 命令是

sed -i '' '/<User id="/d' file.xml

我查看了A regular expression to exclude a word/string和其他一些堆棧溢出帖子,但無法使其正常工作。 請有人幫我寫正則表達式。 我基本上需要刪除所有以 <User id= 開頭的行,但不包括 directoryId="1" 的行

您可以使用

sed -i '' -e '/directoryId="1"/b' -e '/<User id="/d' file.xml

使用此 sed 命令,

  • /directoryId="1"/b跳過包含directoryId="1"
  • /<User id="/d刪除包含<User id="的其他行。

查看在線演示

暫無
暫無

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

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