Red Ant
Red Ant

Reputation: 375

Vim : replacing entire word based on partial match search on all lines

I want to replace all occurrences of a word in a file with a partial match. For example, search using "75008" and replace entire word with "12345678901"

Actual content

75003681846 75008494799 75213014616 
75003681846 75008494795 75213014613                                       
75003681846 75008513200 75213014614                                                   
75003681846 75008494798 75213014617                                                   

Expected content after replace

75003681846 12345678901 75213014616 
75003681846 12345678901 75213014613                                       
75003681846 12345678901 75213014614                                                   
75003681846 12345678901 75213014617                                                   

How to do it in vim

Thanks Red Ant

Upvotes: 0

Views: 852

Answers (2)

Alan Gómez
Alan Gómez

Reputation: 378

For your example also works:

:%s/75008\d\+/12345678901

\d match simple digit, and \+ matches 1 or more of the preceding characters, according to https://vimregex.com/

Upvotes: 0

happydave
happydave

Reputation: 7187

\< and \> match word boundaries.

\S* matches all non-whitespace.

s does substitution.

% applies it to the entire file.

:%s/\<\S*75008\S*\>/12345678901/g

Upvotes: 2

Related Questions