用“SomeWther - ####”替换文件中的所有字符串和“SomeOtherWord-ABC”

问题描述 投票:1回答:1

我必须替换成千上万的包含“SomeWord-”的字符串,后跟四个数字(可以是任意四个数字)。

例如,该文件可能包含:

blah:"someValue1",
otherThing:"someOtherValue1",
importantThing:"SomeWord-1232", 
etc:".....",
importantThing:"SomeWord-4567",
otherThing:"SomeWord-8438"

它需要成为另一个字符串:

blah:"someValue1",
otherThing:"someOtherValue1",
importantThing:"SomeOtherWord-ABC", 
etc:".....",
importantThing:"SomeOtherWord-ABC",
otherThing:"SomeOtherWord-ABC"

使用sed,grep,vim等最简洁的方法是什么?

vim sed grep
1个回答
3
投票

在Vim你可以做到

:%s/SomeWord-\d\+/SomeOtherWord-ABC/g    

匹配一个或多个数字,如果你想要四个,那么你可以做

:%s/SomeWord-\d\{4}/SomeOtherWord-ABC/g  
© www.soinside.com 2019 - 2024. All rights reserved.