Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim search and replace between number

Tags:

vim

I have a pattern where there are double-quotes between numbers in a CSV file. I can search for the pattern by [0-9]\"[0-9], but how do I retain value while removing the double quote. CSV format is like this:

"1234"5678","Text1","Text2"
"987654321","Text3","text4"
"7812891"3","Text5","Text6"

As you may notice there are double quotes between some numbers which I want to remove.

I have tried the following way, which is incorrect:

:%s/[0-9]\"[0-9]/[0-9][0-9]/g

Is it possible to execute a command at every search pattern, maybe go one character forward and delete it. How can "lx" be embedded in search and replace.

like image 361
Sawan Avatar asked Dec 09 '22 14:12

Sawan


2 Answers

You need to capture groups. Try:

:%s/\(\d\)"\(\d\)/\1\2/g

[A digit can also be denoted by \d.]

like image 138
devnull Avatar answered Dec 23 '22 07:12

devnull


I know that this question has been answered already, but here's another approach:

:%s/\d\zs"\ze\d

Explanation:

%s   Substitute for the whole buffer
\d   look up for a digit
\zs set the start of match here
"     look up for a double-quote
\ze set the end of match here
\d   look up for a digit

That makes the substitute command to match only the double-quote surrounded by digits.
Omitting the replacement string just deletes the match.

like image 24
Tassos Avatar answered Dec 23 '22 05:12

Tassos