For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > help in find and replacing a string in a number of files









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 help in find and replacing a string in a number of files
Prabu

2004-04-01, 8:32 am

Hi,
i am new to perl.I am in a problem,So please help me to get a solution
"I want to change texts in 172 files available at a directory
/pages(there are files also in a sub-directory),they all are text files.
There are also some text files,which I no need to change.I want one
single script to open all these files,match the particular
string and replace them with new string.

I tried with a singel file,able to open that file,make changes ,but only
write in another file.I want to write the changes in same old file.


the string needed to find:

<td bgcolor=#336699 valign=top width=10><img src="a web link here"
images/corner_2.gif" width="10" height="10"></td>


the string to be replaced is;

<td bgcolor=#336699 valign=top width=10>
<script>
document.write("<img src=" + url + "/images/corner_2.gif width='10'
height='10'></td>");
</script>
Can anyone help me in this,
With Thanks in advance,
prabu.


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.620 / Virus Database: 399 - Release Date: 3/11/2004



Charles K. Clarkson

2004-04-01, 6:32 pm

prabu <senthilprabu333@yahoo.co.in> wrote:
:
: i am new to perl.I am in a problem,So please help me to
: get a solution
: "I want to change texts in 172 files available at a directory
: /pages(there are files also in a sub-directory),they all are
: text files.
: There are also some text files,which I no need to
: change.I want one
: single script to open all these files,match the particular
: string and replace them with new string.

Read the file 'perlrun' included in your perl distribution.
The section under the -i option gives examples of how to do this.

HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Senthil prabu

2004-04-02, 4:35 am

Hi,
I want to open all (.txt,.html,.js files) files in a particular directory and replace a text in all that files.
What I done so far was a script to replace text in all files in the current directory.
What I want was," When I run the script,it should ask for the path of a particular directory and also the
types(all files of its type in that dir) of files to open.

I wrote a script to change the directory to I specified through commandline.but again it lists only
the files in current directory.
Please help me in this issue,
The code is

print "Enter a path name: ";
my $path=<STDIN>;
chdir($path);

my $file='';
my @files=<*.txt>;

foreach $file (@files){
{
inner loop for pattern matchinh
};


With Thanks inadvance,
Prabu.



Win an evening with the Indian cricket captain: Yahoo! India Promos.
Charles K. Clarkson

2004-04-02, 8:32 am

senthil prabu <senthilprabu333@yahoo.co.in> wrote:

[Please post replies to the list and do not top post]

:
: "Charles K. Clarkson" <cclarkson@htcomp.net> wrote:
: :
: : Read the file 'perlrun' included in your perl
: : distribution. The section under the -i option gives
: : examples of how to do this.
:
: Thanks for the reply. Since I am new to perl, I don't
: know more about perl distribution.

From the command line type: perldoc perldoc


: Please help me to get into it or give some sample
: scripts.

You can:
- look for the 'perlrun' file at perldoc.com,
- Use local html files if you have an ActiveState
install, or
- From the command line type: perldoc perlrun


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Jayakumar Rajagopal

2004-04-02, 10:31 am



-----Original Message-----
From: senthil prabu [mailto:senthilprabu333@yahoo.co.in]
Sent: Friday, April 02, 2004 12:55 AM
To: Charles K. Clarkson; beginners@perl.org
Subject: RE: help in find and replacing a string in a number of files


Hi,
I want to open all (.txt,.html,.js files) files in a particular =
directory and replace a text in all that files.
What I done so far was a script to replace text in all files in the =
current directory.
What I want was," When I run the script,it should ask for the path of a =
particular directory and also the
types(all files of its type in that dir) of files to open.
=20
I wrote a script to change the directory to I specified through =
commandline.but again it lists only
the files in current directory.
Please help me in this issue,
The code is
=20
print "Enter a path name: ";
my $path=3D<STDIN>;
chdir($path);
=20
my $file=3D'';
my @files=3D<*.txt>;
=20
foreach $file (@files){
{
inner loop for pattern matchinh
};
=20
=20
With Thanks inadvance,
Prabu.

Senthil,
Try this in inner loop :
my @files =3D <*>;
if ( ! -d $file && $file =3D~ /\.txt/ )
{do replacement}
else { chdir($file) ; do recuresion that calls this function }

Jay
Sponsored Links







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

Copyright 2008 codecomments.com