0

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
3

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.

  • Wow, that was awesome! – el.pescado Mar 5 '18 at 14:15

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.