Sebbeleu
Sebbeleu

Reputation: 45

Remove symbol after n'th occurrence in Vim

I want to remove semicolons after the 7th occurrence, on multiple rows that may look like this:

foo;10.10.10.10;/24;;NA;;;foo; bar; "foobar"

Meaning that the result should be like this:

foo;10.10.10.10;/24;;NA;;;foo bar "foobar"

What I have been able to do so far is to segment the parts into capture groups:

:%s/(.{-};.{-};.{-};.{-};.{-};.{-};.{-};)(.*)

My problem is that I don't know how to delete characters within a capture group - how do I go about this?

Upvotes: 0

Views: 167

Answers (3)

Alan Gómez
Alan Gómez

Reputation: 378

You also can use this regex:

:%s/\(\(.\{-};\)\{7}\)\|;/\1/g

Upvotes: 0

Artur Siara
Artur Siara

Reputation: 166

if every line has the same format you could simply use macros. Macros also give you an elastic way to the more processing in clearly and intuitive way - you just do what you want in the simplest way you know and vim repeated it. In this example it would be:

lines

foo;10.10.10.10;/24;;NA;;;foo; bar; "foobar"
foo;10.10.10.10;/24;;NA;;;foo; bar; "foobar"
foo;10.10.10.10;/24;;NA;;;foo; bar; "foobar"

record macro

qa08f;xjq

and repeat N-times, eg 1000

1000@a

explanation:

qa - record macro with name a
0 - move cursor to the begging of line
8f; - move cursor to the 8th occurences of semicolon
x - remove semicolon
j - move cursor to the next line
q - finish macro

And repeat macro number of times you need
1000@a

Upvotes: 1

Sato Katsura
Sato Katsura

Reputation: 3086

One way to do it:

:%s/\v^([^;]*;){7}\zs.*/\=substitute(submatch(0), ';', '', 'g')/

Upvotes: 2

Related Questions