vi 搜索 复制粘贴 搜索复制
我只是想知道是否有人可以帮助如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者不询问每次替换的确认:
将产生:
from:
这是如果您想将“--”之后的第一个单词复制到第一个“.”。 并附加“.” 以及最后一个“--”之前找到的单词。
使用 vim。
回复评论:
有人提到,当有多个单词等情况时,它不起作用。
我对它进行了测试:
用上面的表达式替换后:
or without asking confirmation for every replace:
will produce:
from:
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:
after replace with the above expression:
尝试这个:
Try this:
天哪,在我登录发布答案的时候,你发布了它并且已经得到了评论!
--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!
--ab1.cd2..yz99-- -> --ab1.cd2..yz99.ab1--
基于 stefanB 的解决方案,但使用否定字符类和“非常神奇”的设置,我得到以下替换命令:
根据具体要求(“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:
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.