如何在 ed 中使用带有模式的相对行号

发布于 2024-12-04 09:57:08 字数 450 浏览 1 评论 0原文

在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2024-12-11 09:57:08

使用分号 ; 将在处理第二个地址之前设置当前行 .
这使得第二个地址相对于第一个地址,这几乎就是您想要的:

/pattern/-;+2d

因为第二个地址是相对于第一个地址,而不是相对于模式,所以我们必须使用 +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:

/pattern/-;+2d

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)

殊姿 2024-12-11 09:57:08

经过多次尝试和错误,我发现了这一点,尽管我似乎找不到任何可以告诉我这一点的文档。看来每行引用都必须对单独的模式匹配引用进行,因​​此技巧是给该模式两次。

 ed test.txt <<<< 
/pattern/-,/pattern/+d\nwq'

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.

 ed test.txt <<<< 
/pattern/-,/pattern/+d\nwq'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文