comment out all lines in a file
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix questions > comment out all lines in a file




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    comment out all lines in a file  
mike


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-07-04 10:49 PM

Hi,

I have a file that is a few hundred lines long.  Sometimes I need to
comment out all of the lines in the file by placing a "#" charager at
the beginning of each line.

I'm sure there is a single command that I can run from the shell or
from vi to do this for me.

What command will prefix every line in a file with a "#" charater?

Thanks for your help.





[ Post a follow-up to this message ]



    Re: comment out all lines in a file  
Rich Gibbs


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-07-04 10:49 PM

mike said the following, on 10/07/04 14:53:
> Hi,
>
> I have a file that is a few hundred lines long.  Sometimes I need to
> comment out all of the lines in the file by placing a "#" charager at
> the beginning of each line.
>
> I'm sure there is a single command that I can run from the shell or
> from vi to do this for me.
>
> What command will prefix every line in a file with a "#" charater?
>

In vi or vim, hit ':' to enter an editor command. then:
1,$s/^/#/

(The '^' is the meta-character for the beginning of the line.)

--
Rich Gibbs
rgibbs@alumni.princeton.edu





[ Post a follow-up to this message ]



    Re: comment out all lines in a file  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-07-04 10:49 PM

On 2004-10-07, mike wrote:
> Hi,
>
> I have a file that is a few hundred lines long.  Sometimes I need to
> comment out all of the lines in the file by placing a "#" charager at
> the beginning of each line.
>
> I'm sure there is a single command that I can run from the shell or
> from vi to do this for me.
>
> What command will prefix every line in a file with a "#" charater?

sed 's/^/#/' FILE > NEWFILE


--
Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
 ========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License





[ Post a follow-up to this message ]



    Re: comment out all lines in a file  
Kevin Collins


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-07-04 10:49 PM

In article <416597f6@news101.his.com>, Rich Gibbs wrote:
> mike said the following, on 10/07/04 14:53: 
>
> In vi or vim, hit ':' to enter an editor command. then:
>       1,$s/^/#/
>
> (The '^' is the meta-character for the beginning of the line.)

This can be simplified to:

:%s/^/#/

The '%' indicating "all lines", but faster to type than "1,$"...

And conversely you can uncomment the whole file with:

:%s/^#//

Kevin





[ Post a follow-up to this message ]



    Re: comment out all lines in a file  
Greg Orange


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-08-04 07:47 AM

mike wrote:
> What command will prefix every line in a file with a "#" charater?

Well, a simple way in vi:
:%s/^/#/g

You could do it with a PERL (or shell?) script too, if you've got more
than one file to do.

Cheers,
Greg.





[ Post a follow-up to this message ]



    Re: comment out all lines in a file  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-08-04 07:47 AM

2004-10-07, 20:46(+00), Kevin Collins:
[...]
> This can be simplified to:
>
> :%s/^/#/

This can even be simplified to:

:%s/^/#

With vim, you can use visual block selection. In normal mode:
gg<Ctrl-V>GI#<Cr>

(where <Ctrl-V> is pressing Control+V, and <Cr> is pressing
return)

> The '%' indicating "all lines", but faster to type than "1,$"...
>
> And conversely you can uncomment the whole file with:
>
> :%s/^#//

With visual block selection:

gg<Ctrl-V>Gd

--
Stephane





[ Post a follow-up to this message ]



    Re: comment out all lines in a file  
rakesh sharma


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-08-04 12:49 PM

michael_matthes@yahoo.com (mike) wrote in message news:

>
> I have a file that is a few hundred lines long.  Sometimes I need to
> comment out all of the lines in the file by placing a "#" charager at
> the beginning of each line.
>
> I'm sure there is a single command that I can run from the shell or
> from vi to do this for me.
>
> What command will prefix every line in a file with a "#" charater?
>

On the colon prompt within 'vi'

g/^/s//#/





[ Post a follow-up to this message ]



    Re: comment out all lines in a file  
mike


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-08-04 12:49 PM

Thanks everyone.  Your suggestions work great.

michael_matthes@yahoo.com (mike) wrote in message news:<5145db7d.0410071053.13d5313d@posting
.google.com>...
> Hi,
>
> I have a file that is a few hundred lines long.  Sometimes I need to
> comment out all of the lines in the file by placing a "#" charager at
> the beginning of each line.
>
> I'm sure there is a single command that I can run from the shell or
> from vi to do this for me.
>
> What command will prefix every line in a file with a "#" charater?
>
> Thanks for your help.





[ Post a follow-up to this message ]



    Re: comment out all lines in a file  
tosundaram@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-08-04 10:52 PM

sharma__r@hotmail.com (rakesh sharma) wrote in message news:<ed24e4cf.0410080220.70e1f11f@po
sting.google.com>...
> michael_matthes@yahoo.com (mike) wrote in message news:
> 
>
> On the colon prompt within 'vi'
>
>      g/^/s//#/

With PERL you can do like this.

perl -i -npe 's/^/#/'  PERL -i -npe 's/^/#/'





[ Post a follow-up to this message ]



    Re: comment out all lines in a file  
tosundaram@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-08-04 10:52 PM

sharma__r@hotmail.com (rakesh sharma) wrote in message news:<ed24e4cf.0410080220.70e1f11f@po
sting.google.com>...
> michael_matthes@yahoo.com (mike) wrote in message news:
> 
>
> On the colon prompt within 'vi'
>
>      g/^/s//#/

with perl
perl -i -npe 's/^/#/'  <file Name>





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:28 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register