【发布时间】:2019-05-10 15:31:19
【问题描述】:
我正在尝试使用sed 删除一个范围。范围来自已知匹配和接下来的 2 行。假设我想删除所有以Don't 开头的行,然后是后面的 2 行。
请注意,我也在 sed 命令文件中进行替换。因此,我避免在我的解决方案空间中使用-n 和/p。
无论出于何种原因,我都想限制自己只调用一次 sed。
这是我的数据(data.txt):
Print Me
Please Output This line
Don't Print Me and 2 more lines
This line is no good
So is this one
We should see this line
And this one, too.
这是我的预期输出:
Print Me
Please Output This line
We should see this line
And this one, too.
这是一个尝试:
sed -f delete_dont_plus_2.sed data.txt
以此为delete_dont_plus_2.sed:
/^Don't/,+2d
这是我的结果:
sed: 1: delete_dont_plus_2.sed: expected context address
我也试过这些:
/^Don't/,/^Don't/+2d
/^Don't/,{/^Don't/+2}d
这个问题的第二种方法:
假设我们想让这段代码更健壮一些。今天还有2行需要删除,但谁知道将来会有多少行。假设我们要删除最多但不包括We should see this line。在这个问题的变体中,结果完全相同。同样,让我们考虑一个有限的 BSD sed,因此我们不能使用像 /^Don't/,/^We should see this line/-1d 这样的表达式。
谢谢!
【问题讨论】: