简体   繁体   English

在Linux中使用vim替换特定单词

[英]Using vim in Linux to replace specific words

I have a .txt file with information as following: 我有一个.txt文件,其信​​息如下:

...

353 48 338 48 338 9 353 9 **personperson走** 281 64 259 64 259 4 281 4 person

353 48 338 48 338 9 353 9 **personabc** 281 64 259 64 259 4 281 4 person

353 48 338 48 338 9 353 9 **persondef** 281 64 259 64 259 4 281 4 person

...

I want to replace all those bold type words to person (The original words with normal type, not in bold) 我想将所有那些粗体字替换为人(原始字为普通字,而不是粗体)

like this: 像这样:

...

353 48 338 48 338 9 353 9 **person** 281 64 259 64 259 4 281 4 person

353 48 338 48 338 9 353 9 **person** 281 64 259 64 259 4 281 4 person

353 48 338 48 338 9 353 9 **person** 281 64 259 64 259 4 281 4 person

...

I tried use the following command to replace, but the old_string part is different. 我尝试使用以下命令替换,但old_string部分不同。

%s/old_string/new_string/g

Is there any way to use grep in (old_string) part to replace person*(stop at blank) to person 有什么方法可以在(old_string)部分中使用grep替换person *(在空白处停止)

You can use the following in vim: 您可以在vim中使用以下命令:

:%s/person\w\+/person/g

This will replace all words contining person followed by word characters by person 这将替换所有接续人的单词,然后按人替换单词字符

More informations about the substitue function in vim you can find here: http://vim.wikia.com/wiki/Search_and_replace 有关vim中替代功能的更多信息,您可以在这里找到: http : //vim.wikia.com/wiki/Search_and_replace

I would do: 我会做:

%s/\<person\zs\S\+//g
  • This won't replace foopersonbar by fooperson 这不会用foopersonbar代替fooperson
  • This will replace the Chinese word (OP meant "stop at blank )" 这将代替中文单词 (OP的意思是“停在空白处”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM