Misc: perl hack: commenting lines between two markers

Title:

Misc: perl hack: commenting lines between two markers

Author:

Douglas O’Leary <dkoleary@olearycomputers.com>

Description:

Perl one-liner to (un)comment lines between to markers

Date created:

02/2013

Date updated:

02/2013

Disclaimer:

Standard: Use the information that follows at your own risk. If you screw up a system, don’t blame it on me…

I asked this question a couple of years ago on HP’s ITRC and got a wonderful response from James Ferguson. Unfortunately, I can’t seem to remember this little trick to save my life so I’m documenting it here.

Scenario is that certain lines of a script needed to be commented or uncommented depending on the presence of a MC/Serviceguard package. You can search for my question if you’re interested in the mind-fogged attempt I made before the ITRC post, but the answer is:

perl -pe 's/^/# /g if (/mcsg_begins/../mcsg_ends/)' ${file}

I’ve had to search for that post no less than three times since then… You’d figure I’d learn by now.