0

Is there a way to have s;/;|;g run only on the lines I filter using a regex?

I have a file, which looks like:

Date: 2017/08/18
Path /home/share/thing
Path /home/me/thing
Date: 2017/08/18
Path /home/share/thing
Date: 2017/08/18
Path /home/share/thing
Path /home/me/thing
...

Now this is simplified, but the point is I want to substitute the / on the lines beginning with Path, but not the ones beginning with Date. I can grab all of the lines with Path easily enough (:g/Path) and substitute all of the /s (:%s;/;|;g), but for substituting on the specific lines I only know how to give s a range, which doesn't help much.

It seems like it would be easiest if you could pipe the output of g to s, but I couldn't find anything on doing something like that.

5

You can "pipe" the lines from :g/ to :s. Really, this is a special case of using any command after the g//.

g/Path/s;/;|;g

This is also documented explicitly at the bottom of the section :help :g

  • g/^Path/s;/;|;g lines beginning with Path, not lines containing it :) – DarkWiiPlayer Oct 31 '17 at 10:08

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.