Abdullah Nasir
Abdullah Nasir

Reputation: 74

How to search and replace in Vim/Linux if our searching/replacing pattern includes multiple forward slashes?

I have 100K rows file and every row contain date 12/13/2019. I want to replace that date with 12/20/2019. But when I am entering the command like :%s/12/13/2019/12/20/2019/g. It gives an error that couldn't find pattern. Format for date is (MM/DD/YYYY)

Upvotes: 0

Views: 55

Answers (1)

romainl
romainl

Reputation: 196886

A substitution is made of several parts:

:<range>s/<search>/<replace>/<flags>

Between those parts, you have / as default separator. Since / separates the <search> part from the <replace> part, any / in your search pattern is going to be interpreted as a separator, leading to undesirable results.

One solution is to escape your slashes with an anti-slash:

:%s/12\/13\/2019/12\/20\/2019/g

Another one (my favourite) is to use an alternative separator:

:%s@12/13/2019@12/20/2019@g

Reference:

:help :s
:help pattern-delimiter

Upvotes: 1

Related Questions