For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2004 > Line replace









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 Line replace
Urs Wagner

2004-10-11, 8:55 am

Hello

I should comment out lines in a text file. I have a lot of troubles to
realize this.

The lines
Alpha(a, b, c)
should be changed to
# Alpha(a, b, c)

What I would like to write is a small perl script to make this convetion.

Thanks

Urs
Ramprasad A Padmanabhan

2004-10-11, 8:55 am

On Mon, 2004-10-11 at 16:23, Urs Wagner wrote:
> Hello
>
> I should comment out lines in a text file. I have a lot of troubles to
> realize this.
>
> The lines
> Alpha(a, b, c)
> should be changed to
> # Alpha(a, b, c)
>



perl -pli.BAK -e 's/(?=.*Alpha\(a, b ,c\))/#/' FILENAME

HTH
Ram


Arjun Mallik

2004-10-11, 8:55 am


Good one liner ....I appreciate it ..

Arjun


-----Original Message-----
From: Rampra A Padmanabhan [mailto:rampra.padmanabhan@oracle.com]=0D
Sent: Monday, October 11, 2004 4:41 PM
To: Urs Wagner
Cc: perl beginners
Subject: Re: Line replace


On Mon, 2004-10-11 at 16:23, Urs Wagner wrote:
> Hello
>=0D
> I should comment out lines in a text file. I have a lot of troubles to
> realize this.
>=0D
> The lines
> Alpha(a, b, c)
> should be changed to
> # Alpha(a, b, c)
>=0D



perl -pli.BAK -e 's/(?=3D.*Alpha\(a, b ,c\))/#/' FILENAME

HTH
Ram



--=0D
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Confidentiality Notice=0D

The information contained in this electronic message and any attachments to=
this message are intended
for the exclusive use of the addressee(s) and may contain confidential or=
privileged information. If
you are not the intended recipient, please notify the sender at Wipro or=
Mailadmin@wipro.com immediately
and destroy all copies of this message and any attachments.
Arjun Mallik

2004-10-11, 8:55 am


Hai !
Open the file with any file handler and use regular expression.. This
way it will work.



Open (File handler name ,">>file name");

While ( $string=3D<filehandler> )
{
if ( $string =3D ~ /^Alpha\(a,b,c\)/ )
{
$string =3D "#" . $string ;
print <file handler> $string ;
}
}


Arjun

-----Original Message-----
From: Urs Wagner [mailto:wagner@itp.phys.ethz.ch]=0D
Sent: Monday, October 11, 2004 4:24 PM
To: beginners@perl.org
Subject: Line replace


Hello

I should comment out lines in a text file. I have a lot of troubles to=0D
realize this.

The lines
Alpha(a, b, c)
should be changed to
# Alpha(a, b, c)

What I would like to write is a small perl script to make this
convetion.

Thanks

Urs

--=0D
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>





Confidentiality Notice=0D

The information contained in this electronic message and any attachments to=
this message are intended
for the exclusive use of the addressee(s) and may contain confidential or=
privileged information. If
you are not the intended recipient, please notify the sender at Wipro or=
Mailadmin@wipro.com immediately
and destroy all copies of this message and any attachments.
Chris Devers

2004-10-11, 3:55 pm

On Mon, 11 Oct 2004, Rampra A Padmanabhan wrote:

> On Mon, 2004-10-11 at 16:23, Urs Wagner wrote:
>
> perl -pli.BAK -e 's/(?=.*Alpha\(a, b ,c\))/#/' FILENAME


Knowing how to do this in Perl is, of course, a very useful thing.

In this case though, it would also be useful to know how to do this in
your text editor. In vi/Vim, for example, you can do a --

:%s/\(Alpha(a, b, c)\)/# \1/gc

-- and this will find every instance of the string [1], show you each
instance as it is found, and ask for you to confirm the change. If it
looks right when the command finishes, save the file as normal.

I know Emacs can do this about as efficiently, but I'm not an Emacs user
so I'm not sure how to go about it.

Editors like BBEdit, UltraEdit, and even Microsoft Write / WordPad have
ways to get similar results, though it may be by way of simple replace
rather than a full regular expression match & substitution. In this
case, that's okay -- you can work with literal strings here -- but using
an editor that can do full regex search & replace is very useful. It's
worth using such an editor and getting comfortable with how it works.


[1] "Every instance" includes multiple instances on the same line,
which may or may not be what you want; take out the 'g' at the
end of the command if you only want to match the first one.


--
Chris Devers
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com