I was just wondering if anyone could help out with how to do the following using vi.
我只是想知道是否有人可以帮助如何使用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 --
如果我想取这行并通过搜索与第一次出现的[A-Za-z0-9]副本匹配到缓冲区的任何内容进行转换,然后将它附加到第一个出现之前的同一行 -
start of text file:
--something1.something2.something1--
--anotherThing1.something2.anotherThing1--
end of text file:
Is there a single VI command to do this?
是否有一个VI命令来执行此操作?
Cheers Ben
4 个解决方案
#1
:%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:
#2
Try this:
%s:\([A-Za-z0-9]\+\)\(\..\+\)--:\1\2.\1--:g
#3
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--
--ab1.cd2..yz99-- - > --ab1.cd2..yz99.ab1--
#4
Building on stefanB's solution but using negated character classes and the "very magic" setting, I arrive at the following substitution command:
在stefanB的解决方案的基础上,但使用否定的字符类和“非常神奇”的设置,我得到以下替换命令:
:%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.
根据具体要求(“something1”和“anotherThing1”允许的字符),这可能会或可能不会更正确。
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.
还有一件事需要考虑:到目前为止发布的所有解决方案都假设每行只有一个“--text.someOtherText--模式”。如果不是这种情况,模式的(。*)部分会有要调整,需要/ g修饰符。
#1
:%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:
#2
Try this:
%s:\([A-Za-z0-9]\+\)\(\..\+\)--:\1\2.\1--:g
#3
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--
--ab1.cd2..yz99-- - > --ab1.cd2..yz99.ab1--
#4
Building on stefanB's solution but using negated character classes and the "very magic" setting, I arrive at the following substitution command:
在stefanB的解决方案的基础上,但使用否定的字符类和“非常神奇”的设置,我得到以下替换命令:
:%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.
根据具体要求(“something1”和“anotherThing1”允许的字符),这可能会或可能不会更正确。
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.
还有一件事需要考虑:到目前为止发布的所有解决方案都假设每行只有一个“--text.someOtherText--模式”。如果不是这种情况,模式的(。*)部分会有要调整,需要/ g修饰符。