如何在 ed 中使用带有模式的相对行号
在尝试使用 ed 删除某个模式周围的行时,我一直在发疯 ts。
我想做的是匹配一个模式,然后删除它周围的行。
我尝试了几种变体,
ed test.txt <<<< $'/pattern/-1,+1d\nwq'
ed test.txt <<<< $'(/pattern/-1,+1)d\nwq'
ed test.txt <<<< $'/pattern/-,+1d\nwq'
ed test.txt <<<< $'(/pattern/-,+1)d\nwq'
ed test.txt <<<< $'/pattern/-,+d\nwq'
ed test.txt <<<< $'(/pattern/-,+)d\nwq'
但没有一个有效。它是如何完成的?
In attempting to use ed
to delete lines around a certain pattern I've been driving my self nut
ts.
What I'd like to do is match a pattern, and then delete lines around it.
I've tried several variations
ed test.txt <<<<
None of which worked. How is it done?
/pattern/-1,+1d\nwq'
ed test.txt <<<<
None of which worked. How is it done?
(/pattern/-1,+1)d\nwq'
ed test.txt <<<<
None of which worked. How is it done?
/pattern/-,+1d\nwq'
ed test.txt <<<<
None of which worked. How is it done?
(/pattern/-,+1)d\nwq'
ed test.txt <<<<
None of which worked. How is it done?
/pattern/-,+d\nwq'
ed test.txt <<<<
None of which worked. How is it done?
(/pattern/-,+)d\nwq'
None of which worked. How is it done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用分号
;
将在处理第二个地址之前设置当前行.
。这使得第二个地址相对于第一个地址,这几乎就是您想要的:
因为第二个地址是相对于第一个地址,而不是相对于模式,所以我们必须使用 +2 来寻址图案前一根线和后一根线。
(请注意,
/pattern/-
是/pattern/-1
的简写)Using a semicolon,
;
, will set the current line,.
, before processing the second address.This makes the second address relative to the first, which is almost what you want:
Because the second address is relative to the first, and not relative to the pattern, we have to use +2 to address the lines one-before and one-after the pattern.
(Note that
/pattern/-
is shorthand for/pattern/-1
)经过多次尝试和错误,我发现了这一点,尽管我似乎找不到任何可以告诉我这一点的文档。看来每行引用都必须对单独的模式匹配引用进行,因此技巧是给该模式两次。
I figured it out after much trial and error, though I can't seem to find any documentation that would have told me this. It appears that each line reference must be made to a separate pattern match reference, and so the trick is to give the pattern twice.