vi 搜索 复制粘贴 搜索复制

发布于 2024-07-25 07:33:35 字数 473 浏览 5 评论 0原文

我只是想知道是否有人可以帮助如何使用 vi 执行以下操作。

我有一个文本文件,它可能包含类似的内容

start of text file:
--something1.something2--
--anotherThing1.something2--
end of text file:

如果我想获取此行并通过搜索与第一次出现的 [A-Za-z0-9] 匹配的任何内容来转换它复制该内容到缓冲区,然后将其附加到第一次出现之前的同一行上 --

start of text file:
--something1.something2.something1--
--anotherThing1.something2.anotherThing1--
end of text file:

是否有一个 VI 命令可以执行此操作?

干杯 本

I was just wondering if anyone could help out with how to do the following using vi.

I have a text file and it might contain something like

start of text file:
--something1.something2--
--anotherThing1.something2--
end of text file:

If I want to take this line and convert it by way of searching for anything matching the first occurrence of [A-Za-z0-9] copy that to the buffer then append it on the same line to before the first occurrent of --

start of text file:
--something1.something2.something1--
--anotherThing1.something2.anotherThing1--
end of text file:

Is there a single VI command to do this?

Cheers
Ben

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

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

发布评论

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

评论(4

花海 2024-08-01 07:33:36
:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/gc

或者不询问每次替换的确认:

:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/g

将产生:

--something1.something2.something1--
--anotherThing1.something2.anotherThing1--

from:

--something1.something2--
--anotherThing1.something2--

这是如果您想将“--”之后的第一个单词复制到第一个“.”。 并附加“.” 以及最后一个“--”之前找到的单词。

使用 vim。

回复评论:

有人提到,当有多个单词等情况时,它不起作用。
我对它进行了测试:

start of text file:
--something1.something2.something3.something4.something5.something6--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5--
end of text file:

用上面的表达式替换后:

start of text file:
--something1.something2.something3.something4.something5.something6.something1--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5.anotherThing1--
end of text file:
:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/gc

or without asking confirmation for every replace:

:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/g

will produce:

--something1.something2.something1--
--anotherThing1.something2.anotherThing1--

from:

--something1.something2--
--anotherThing1.something2--

This is if you want to copy the first word after '--' up to first '.' and append '.' and word found before the last '--'.

Using vim.

RE COMMENTS:

Someone mentioned that it will not work when there are multiple words and so on.
I tested it on the following:

start of text file:
--something1.something2.something3.something4.something5.something6--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5--
end of text file:

after replace with the above expression:

start of text file:
--something1.something2.something3.something4.something5.something6.something1--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5.anotherThing1--
end of text file:
别再吹冷风 2024-08-01 07:33:36

尝试这个:

%s:\([A-Za-z0-9]\+\)\(\..\+\)--:\1\2.\1--:g

Try this:

%s:\([A-Za-z0-9]\+\)\(\..\+\)--:\1\2.\1--:g
暮色兮凉城 2024-08-01 07:33:36

天哪,在我登录发布答案的时候,你发布了它并且已经得到了评论!

%s/--\(\w*\)\.\(.*\)--/--\1.\2.\1--/g

--ab1.cd2..yz99-- -> --ab1.cd2..yz99.ab1--

Holy crap, in the time it took me to login to post that answer, you posted it and already got a comment!

%s/--\(\w*\)\.\(.*\)--/--\1.\2.\1--/g

--ab1.cd2..yz99-- -> --ab1.cd2..yz99.ab1--

甜味超标? 2024-08-01 07:33:36

基于 stefanB 的解决方案,但使用否定字符类和“非常神奇”的设置,我得到以下替换命令:

:%s/\v--([^.]+)\.(.*)--/--\1.\2.\1--/

根据具体要求(“something1”和“anotherThing1”允许的字符),这可能是也可能不是更正确。

还有一件事需要考虑:到目前为止发布的所有解决方案都假设每行仅出现一次“--text.someOtherText--”模式。如果不是这种情况,则模式的 (.*) 部分将具有进行调整,并且需要 /g 修饰符。

Building on stefanB's solution but using negated character classes and the "very magic" setting, I arrive at the following substitution command:

:%s/\v--([^.]+)\.(.*)--/--\1.\2.\1--/

Depending on the exact requirements (what are allowed characters for "something1" and "anotherThing1") this might or might not be more correct.

One more thing to consider: all solutions posted so far assume that there is only one occurance of the "--text.someOtherText-- pattern per line. If this is not the case, the (.*) part of the pattern would have to be adjusted and the /g modifier is required.

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