I've got a file with all Belgian cities. Some of these cities have boroughs.


But I want to divide the boroughs with , and space instead of ;. So I want to get this:

Lier;stad;Antwerpen;49,70;33930;Lier, Koningshooikt
Knokke-Heist;gem.;West-Vlaanderen;56,44;33825;Knokke, Westkapelle, Heist, Ramskapelle

I use vi regularly but this kind of substitution is hard for me.

2 Answers 2


You could do (assuming GNU sed):

:%!sed 's/;/, /6g'

To replace the 6th to last ; with ,.

With vim using look-behind operators:

:%s/\v((;.*){5})@<=;/, /g

(that is replace the ;s that are preceded by at least 5 ;s).

  • It seems to work but what should I add so it would start from the 6th and go on instead change only the 6th? EDIT: the g of course :). THanks Dec 21, 2015 at 16:45

Another way in vim:

:g/^/call feedkeys("nnnnna") | s/;/, /gc

The feedkeys() function use the string nnnnna as if we typed them on keyboard, substitution with gc flags tell vim to substitute all occurrences with confirmation from user.

Here we simply ignored first 5 occurrences by nnnnn, then an a confirm all others.

The g/^/ made this command applied for all lines.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .