如何使用vim命令与SED进行括号? - 除非第一个字符发行

发布于 2025-02-09 00:54:44 字数 974 浏览 0 评论 0原文

我有这个配置文件,我想从中展开它的括号。 据我所知,我可以最好地使用vim -e -c'supper tose''

输入文件

["12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",["127.0.0.1:12000"]]

应该看起来像这样。

所需的输出

[
"12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",
[
"127.0.0.1:12000"
]
]

实际上,这只是第一步,但是我已经使用gg = g来确定了这些输出。

在VIM本身中测试时使用的此命令:

:%s/^\[/\[\r/|%s/[^^]\[/\r\[\r/g|%s/\]/\r\]/g

以下是我现在可以看到

输出文件

[
"12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000"
[
"127.0.0.1:12000"
]
]

的主要问题是我的支架替换sed命令替换了两个字符而不是一个字符。我认为s/[^^] \ [/\ r \ [\ r/g我代码的一部分是错误的。

它查看两个字符的图案,第一个字符不应是该行的第一个字符,第二个字符应该是开放式括号。

但是,那不是我想要的。我只想查看打开括号,只有在它们不是第一个字符时才替换它们。

我该怎么做?

I've got this config file that I'd like to unfold it's brackets from.
As far as I can tell I can best use the vim -e -c 'stuff goes here' command for this and put that inside a shell script.

Input file

["12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",["127.0.0.1:12000"]]

It should look like this.

Desired output

[
"12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",
[
"127.0.0.1:12000"
]
]

Well actually, that's just the first step, but I've got those mostly figured out using gg=G.

This command used when testing in vim itself:

:%s/^\[/\[\r/|%s/[^^]\[/\r\[\r/g|%s/\]/\r\]/g

And below is what I get to see

Output file

[
"12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000"
[
"127.0.0.1:12000"
]
]

My main issue right now is that my bracket replacement sed command replaces two characters instead of one. I figured that the s/[^^]\[/\r\[\r/g part of my code is wrong.

It looks at a pattern of two characters, a first character should not be the first character of the line and a second character should be an opening bracket.

That's however not what I want. I just want to look at opening brackets and only replace them if they are not the first character.

How would I do that?

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

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

发布评论

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

评论(1

小嗷兮 2025-02-16 00:54:44

SED可以工作

$ sed s'/\[\([^][]*\)/[\n\1\n/g;s/]/&\n/' input_file
[
"12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",
[
"127.0.0.1:12000"
]
]

vim

:s/\[\([^][]*\)/[\r\1\r/g|s/]/&\r/

This sed may work

$ sed s'/\[\([^][]*\)/[\n\1\n/g;s/]/&\n/' input_file
[
"12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",
[
"127.0.0.1:12000"
]
]

Or vim

:s/\[\([^][]*\)/[\r\1\r/g|s/]/&\r/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文