SPUG: replace a text to other inside a file

Umar Cheema umar at drizzle.com
Tue Dec 28 02:44:03 CST 2004


Good point.

On Tue, 28 Dec 2004, Ivan Heffner wrote:

> Except Perl will treat the unescaped '.'s in the first half of the regex as 
> "any single character" which probably is not quite what you want.  Try
> 
> perl -pi -e 's[\.{2}/(\w+)\.txt][../../$1]g' some_file.txt
> 
> Ivan
> 
> At 12/27/2004 11:53 PM, Umar Cheema wrote:
> >Oops, just noticed that you also had another directory level added to the
> >replaced pattern. You can try this simple regex:
> >
> >perl -pi.bak -e 's/..\/(\w+).txt$/..\/..\/$1/g' some_file.txt
> >
> >
> >
> >On Mon, 27 Dec 2004, Umar Cheema wrote:
> >
> > > I usually take care of these things by running a perl regex on the command
> > > line:
> > >
> > > $ perl -pi.bak -e 's/\.txt$//g' some_file.txt
> > >
> > > where 'some_file.txt' is your file that contains some text. You can
> > > modify the regex to match your requirements and/or file's content
> > > structure (this regex will not work if you expect to have multiple .txt
> > > patterns on the same line or if .txt is not the last thing on the line, 
> > etc).
> > > You can also pass in *.txt as an argument to replace your text in multiple
> > > files.
> > >
> > > The .bak will create backups of your original file (some_file.txt.bak)
> > > incase you make a mistake. You can leave the .bak part out if you're a
> > > confident and fearless man who laughs at the word 'mistake'.
> > >
> > >
> > > Umar
> > >
> > >
> > > On Tue, 28 Dec 2004, Sachin Chaturvedi wrote:
> > >
> > > > i want to change some text inside file to other using perl
> > > > for example
> > > >
> > > > file contains
> > > >
> > > > source ../setextreme.txt
> > > >
> > > > and i want to change it to
> > > >
> > > > source ../../setextreme
> > > >
> > > > can u tell me if i can do using perl script.
> > > >
> > > > i am not getting much help from text book
> > > > _____________________________________________________________
> > > > Seattle Perl Users Group Mailing List
> > > >      POST TO: spug-list at mail.pm.org
> > > > SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
> > > >     MEETINGS: 3rd Tuesdays, Location: Amazon.com Pac-Med
> > > >     WEB PAGE: http://seattleperl.org/
> > > >
> > >
> > >
> > >
> > >
> >
> >
> >
> >_____________________________________________________________
> >Seattle Perl Users Group Mailing List
> >      POST TO: spug-list at mail.pm.org
> >SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
> >     MEETINGS: 3rd Tuesdays, Location: Amazon.com Pac-Med
> >     WEB PAGE: http://seattleperl.org/
> 





More information about the spug-list mailing list