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

> On Mon, 2004-10-11 at 16:23, Urs Wagner wrote:
> > 
> > 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
 
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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to