ginju75 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have a large input file, in which I have to comment out a few lines. It is a pain to do it manually, so I wanted to write a script to do it.

So here is what I have:

The block of line which I want to comment out start with a TAG, and end with a ");". For example:

MYTAG (
Line 1
Line 2
Line 3
);

So, the block of lines would start with a tag and end with ");" And there can be amny block of lines like these, ofcourse each with an individual starting TAG

My question is how will I read just the required lines between the TAG and the closing bracket and then just comment out those lines in the input.

Effectively, the above lines in the input text need to be changed to:

#MYTAG (
# Line 1
# Line 2
# Line 3
# );

Please provide some wisdom..

Thank you all great Monks,

A learning Monk!

Replies are listed 'Best First'.
Re: How to comment out lines in a text?
by zengargoyle (Deacon) on Mar 23, 2002 at 00:36 UTC
    $ cat TAG MYTAG ( Line 1 Line 2 Line 3 ); NOTMYTAG ( foo bat ); MYTAG ( Line 1 Line 2 Line 3 ); $ perl -n -e 'BEGIN{$com=""}' -e '$com="#"if/^MYTAG\W/;print"$com$_";$ +com=""if/^\);$/' <TAG #MYTAG ( #Line 1 #Line 2 #Line 3 #); NOTMYTAG ( foo bat ); #MYTAG ( #Line 1 #Line 2 #Line 3 #);
      Golf?
      perl -pe'$_="#$_"if/^MYTAG\W/../^\);$/'<TAG
      Hope you don't mind me using your regexes. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

        Not at all.....

        perl -pe's/^/#/ if/^MYTAG\W/../^\);$/'<TAG

        ;)

Re: How to comment out lines in a text?
by jeffenstein (Hermit) on Mar 23, 2002 at 21:43 UTC
    sed -e '/^MYTAG/,/);/s/^/#/' < input.txt > output.txt