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

时间:2014-03-08 05:53:20

标签: bash sed

我需要删除包含a-Z0-9或符号或{{1}的所有行}来自文件,但只有在大括号({)内的某处找到它们,也可以在同一行找到。

例如,取样本文件:

}
  • 第1行和第2行将被删除,因为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. 的项目位于大括号之间。
  • 第4行(和第2行)将被删除,因为数字位于大括号之间。
  • 第8行将被删除,因为a-Z存在并且在大括号内。
  • 其他行会被忽略,因为他们都没有将这些项放在{括号之间。

我尝试制作这个BASH脚本,但在仔细检查输出后,找到了一些不起作用的实例:

}
  • 大括号永远不会嵌套。
  • 大括号永远不会分开。

为什么我的sed '/{.*[a-Z0-9《》【】].*}/d' input.txt > output.txt 脚本无法正确删除包含seda-Z0-9的所有行,或者在大括号之间找到

1 个答案:

答案 0 :(得分:1)

您的角色类不正确[a-Z]。它应该是[a-z][A-Z]或两者[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, 《水滸傳》.