2

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

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

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.

3

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 – M-Reda Daoudi Dec 21 '15 at 16:45
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.