Unix questions - comment out all lines in a file

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > October 2004 > comment out all lines in a file





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author comment out all lines in a file
mike

2004-10-07, 5: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.
Rich Gibbs

2004-10-07, 5: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
Chris F.A. Johnson

2004-10-07, 5: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
Kevin Collins

2004-10-07, 5: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
Greg Orange

2004-10-08, 2: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.
Stephane CHAZELAS

2004-10-08, 2: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
rakesh sharma

2004-10-08, 7:49 am

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//#/
mike

2004-10-08, 7:49 am

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.

tosundaram@gmail.com

2004-10-08, 5:52 pm

sharma__r@hotmail.com (rakesh sharma) wrote in message news:<ed24e4cf.0410080220.70e1f11f@posting.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/^/#/'
tosundaram@gmail.com

2004-10-08, 5:52 pm

sharma__r@hotmail.com (rakesh sharma) wrote in message news:<ed24e4cf.0410080220.70e1f11f@posting.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>
Stephane CHAZELAS

2004-10-11, 2:47 am

2004-10-8, 06:59(-07), tosundaram@gmail.com:
[...]
> PERL -i -npe 's/^/#/' <file Name>


There's no point in having the -n and -p options at the same
time.

perl -i -pe 's/^/#/' file

Note that you may lose data with that command, if PERL fails in
the process (for instance if there's no space left on the
filesystem). You may prefer using the -i.bak option.

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com