我只是想知道是否有人可以帮助解决如何使用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命令可以做到这一点?
干杯本
发布于 2009-06-24 15:56:55
:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/gc或者不询问每次替换的确认:
:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/g将产生:
--something1.something2.something1--
--anotherThing1.something2.anotherThing1--发自:
--something1.something2--
--anotherThing1.something2--这是如果您想要复制“--”up to first“之后的第一个单词。”并附加'.‘在最后一个“--”之前找到的单词。
使用vim。
RE评论:
有人提到,当有多个单词等情况下,它将不起作用。我在以下几个方面进行了测试:
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:发布于 2009-06-24 16:02:03
我的天啊,在我登录到发布答案的时间里,你发布了它,并且已经得到了评论!
%s/--\(\w*\)\.\(.*\)--/--\1.\2.\1--/g--ab1.cd2..yz99 -> --ab1.cd2..yz99.ab1--
发布于 2009-06-24 16:06:07
试试这个:
%s:\([A-Za-z0-9]\+\)\(\..\+\)--:\1\2.\1--:ghttps://stackoverflow.com/questions/1039212
复制相似问题