簡體   English   中英

如何用sed刪除包含模式“ {。* [a-Z0-9《》【】【。]。*}“】的行?

[英]How to delete lines containing the pattern `{.*[a-Z0-9《》【】].*}` with sed?

我需要從文件中刪除包含aZ0-9或符號中的任何一行的所有行,但前提是必須在括號( {} )內找到它們。同一行。

例如,獲取示例文件:

Once {upon} a time, there lived an owl.
The owl lived in a {forest with 10,000 trees.}
One day, the owl said, "I like to eat mice."
So the owl ate {10,000} mice.
The owl said, {“吃飽了!”}
Then{,} the owl rested.
The next day, he read the book, 《水滸傳》.
He enjoyed it so much, that he read {《水滸傳》} again the next day.
  • 第1行和第2行將被刪除,因為在括號之間的某處發現了帶有aZ項目。
  • 第4行(和第2行)將被刪除,因為在括號之間找到了數字。
  • 8號線將被刪除,因為存在並且在大括號。
  • 其他行將被忽略,因為它們都沒有放在{}大括號之間。

我嘗試制作此BASH腳本,但是在仔細檢查了輸出之后,發現了一些實例,這些實例不起作用:

sed '/{.*[a-Z0-9《》【】].*}/d' input.txt > output.txt
  • 大括號永遠不會嵌套。
  • 大括號永遠不會在一行上分開。

為什么在括號之間發現我的sed腳本不能正確刪除aZ0-9 所有行?

您的角色類別不正確[aZ] 它應該是[az][AZ]或都為[a-zA-Z]

sed '/{[^}]*[a-zA-Z0-9《》【】][^}]*}/d' file

$ cat file
Once {upon} a time, there lived an owl.
The owl lived in a {forest with 10,000 trees.}
One day, the owl said, "I like to eat mice."
So the owl ate {10,000} mice.
The owl said, {“吃飽了!”}
Then{,} the owl rested.
The next day, he read the book, 《水滸傳》.
He enjoyed it so much, that he read {《水滸傳》} again the next day.

$ sed '/{[^}]*[a-zA-Z0-9《》【】][^}]*}/d' file
One day, the owl said, "I like to eat mice."
The owl said, {“吃飽了!”}
Then{,} the owl rested.
The next day, he read the book, 《水滸傳》.

暫無
暫無

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

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