divided
divided

Reputation: 1319

Duplicate partial line text with vi/vim

I have two lists like these below:

abc.domain.com
defg.domain.com
hijklmn.domain.com
abc.domain.com/sub/
defg.domain.com/anothersub/
hijklmn.domain.com/thirdsub/

I need to make these two lists appear like so:

abc.domain.com?ref=abc
defg.domain.com?ref=defg
hijklmn.domain.com?ref=hijklmn
abc.domain.com/sub/?ref=sub
defg.domain.com/anothersub/?ref=anothersub
hijklmn.domain.com/thirdsub/?ref=thirdsub

How to I accomplish this in vi/vim?

Upvotes: 2

Views: 50

Answers (1)

Kent
Kent

Reputation: 195269

Assume the two lists are in two different buffers. We can do the job using vim's :s command:

For list1:

%s/\v([^.]+).*/&?ref=\1/

For list2:

%s#\v.*/([^/]+)/$#&?ref=\1#

If the two lists are mixed in one buffer, you can use :g command to do the substitution only on a certain target.

Upvotes: 6

Related Questions