user13052921
user13052921

Reputation: 21

how to replace a particular string on multiple line in vi/vim editor

I have the following lines in a file :

|other words in line| abcd_1234.xyz |other words in line|

|other words in line| abcd_2345.xyz |other words in line|

|other words in line| abcd_3456.xyz |other words in line|

|other words in line| abcd_4567.xyz |other words in line|

I want to change the above lines to

|other words in line| q |other words in line|

|other words in line| q |other words in line|

|other words in line| q |other words in line|

|other words in line| q |other words in line|

I tried :%s/abcd*.xyz/q/g but that does not seem to work. Please can anyone suggest how to go about this.

Upvotes: 1

Views: 1488

Answers (3)

Alan Gómez
Alan Gómez

Reputation: 378

You must use:

:%s/abcd.*\.xyz/q/g

For match . you need to write as\., remember a simple dot . is also a command.

Upvotes: 0

Md Golam Rahman Tushar
Md Golam Rahman Tushar

Reputation: 2375

Try the following:

:%s/(abcd_[0-9]{4}\.xyz)/q/g 

Upvotes: 3

malarres
malarres

Reputation: 2946

If we can count on having the spaces and the | then you can use:

:%s/\|(.*)\| (abcd_.*) \|(.*)\|/\1q\3

PS: you can use https://regex101.com/r/DHLbIx/1 to help you with the matching

Upvotes: 0

Related Questions