与SED匹配后如何替换2条线

发布于 2025-01-18 11:52:22 字数 413 浏览 1 评论 0原文

在使用SED发现比赛后,有没有办法替换以下两条线?

我有一个文件

#ABC
oneSize=bar
twoSize=bar
threeSize=foo

,但是一旦模式#abc匹配,我想替换两个即时

#ABC
oneSize=foo
twoSize=foo
threeSize=foo

gsed'/^#abc/{n; s/size = bar/size = foo/}'文件

但它仅更改行twosize not not oneyize < /code>

是否有一种方法可以使其更改OneSize和Twosize

Is there a way to replace the following two lines after a match is found using sed?

I have a file

#ABC
oneSize=bar
twoSize=bar
threeSize=foo

But I would like to replace the two immediate lines once the pattern #ABC is matched so that it becomes

#ABC
oneSize=foo
twoSize=foo
threeSize=foo

I'm able to do
gsed '/^#ABC/{n;s/Size=bar/Size=foo/}' file

But it only changes the line twoSize not oneSize

Is there a way to get it to change both oneSize and twoSize

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

分分钟 2025-01-25 11:52:22

您可以重复这些命令:

gsed '/^#ABC/{n;s/Size=bar/Size=foo/;n;s/Size=bar/Size=foo/}' file

请参阅在线演示

n 命令 "打印模式空间,然后无论如何,用下一行输入替换模式空间。如果没有更多输入,则 sed 退出而不处理任何其他命令。

因此,第一次使用它时,您将替换它。在以以下开头的行之后的第一行#ABC,然后替换该行下面的第二行。

You can repeat the commands:

gsed '/^#ABC/{n;s/Size=bar/Size=foo/;n;s/Size=bar/Size=foo/}' file

See the online demo.

The n command "prints the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed exits without processing any more commands."

So, the first time it is used, you replace on the first line after line starting with #ABC, then you replace on the second line below that one.

国粹 2025-01-25 11:52:22

GNU和其他一些SED版本允许您使用相对号码抓住范围,因此您可以简单地使用:

sed -E '/^#ABC$/,+2 s/(Size=)bar/\1foo/' file
#ABC
oneSize=foo
twoSize=foo
threeSize=foo

命令详细信息:

  • /^#ABC $/,+2匹配范围来自模式#ABC到下一个2行
  • s/(size =)bar/\ 1foo/:match size = size = bar并用替换size = foo,使用捕获组避免在搜索和替换中重复同一字符串,

您也可以考虑awk,以避免重复模式和替换n次,如果您必须在之后替换n行匹配模式:

awk 'n-- {sub(/Size=bar/, "Size=foo")} /^#ABC$/ {n=2} 1' file

#ABC
oneSize=foo
twoSize=foo
threeSize=foo

gnu and some other sed versions allow you to grab a range using relative number so you can simply use:

sed -E '/^#ABC$/,+2 s/(Size=)bar/\1foo/' file
#ABC
oneSize=foo
twoSize=foo
threeSize=foo

Command details:

  • /^#ABC$/,+2 Match range from pattern #ABC to next 2 lines
  • s/(Size=)bar/\1foo/: Match Size=bar and replace with Size=foo, using a capture group to avoid repeat of same String in search and substitution

You may also consider awk to avoid repeating pattern and replacements N times if you have to replace N lines after matching pattern:

awk 'n-- {sub(/Size=bar/, "Size=foo")} /^#ABC$/ {n=2} 1' file

#ABC
oneSize=foo
twoSize=foo
threeSize=foo
仙女 2025-01-25 11:52:22

使用sed,当不再找到Size=bar时,循环将中断,因此替换匹配后的前两行。

$ sed '/^#ABC/{:l;n;s/Size=bar/Size=foo/;tl}' input_file
#ABC
oneSize=foo
twoSize=foo
threeSize=foo

Using sed, the loop will break when Size=bar is no longer found therefore replacing the first 2 lines after the match.

$ sed '/^#ABC/{:l;n;s/Size=bar/Size=foo/;tl}' input_file
#ABC
oneSize=foo
twoSize=foo
threeSize=foo
塔塔猫 2025-01-25 11:52:22

使用sed -z

sed -z 's/#ABC\noneSize=bar\ntwoSize=bar/#ABC\noneSize=foo\ntwoSize=foo/' file.txt
#ABC
oneSize=foo
twoSize=foo
threeSize=foo

sed -E -z 's/#ABC\n(.*)bar\n(.*)bar/#ABC\n\1foo\n\2foo/' file.txt

Using sed -z

sed -z 's/#ABC\noneSize=bar\ntwoSize=bar/#ABC\noneSize=foo\ntwoSize=foo/' file.txt
#ABC
oneSize=foo
twoSize=foo
threeSize=foo

Or

sed -E -z 's/#ABC\n(.*)bar\n(.*)bar/#ABC\n\1foo\n\2foo/' file.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文