简体   繁体   中英

Using vim in Linux to replace specific words

I have a .txt file with information as following:

...

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.

%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

You can use the following in 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

I would do:

%s/\<person\zs\S\+//g
  • This won't replace foopersonbar by fooperson
  • This will replace the Chinese word (OP meant "stop at blank )"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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