1

I want to search file for pattern, duplicate every line that matches that pattern, then in perform substitution on each duplicated line.

Suppose file looks like this:

a 1
a 2
b 1
b 2
c 1
c 2

I want to duplicate each line matching /2, then substitute 2 with 3 (s/2/3), and end up with file:

a 1
a 2
a 3
b 1
b 2
b 3
c 1
c 2
c 3

1 Answer 1

5

You can do that with a :g command which marks all lines on which to act and then run a combination of a :t (copy) and :s (substitute) command.

Something like this should work:

g/2$/t.|s/2/3

This executes on each line matching 2$ the following command: :t.|s/2/3 which means, copy the current line and afterwards replace the 2 by 3 in the current (copied) line.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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