Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I replace all occurances a "word" in the file and filename

Status
Not open for further replies.

Relative0

Programmer
Feb 22, 2005
1
US
What I would like to do is take a directory and replace every single occurance of a string in a file as well as in file names. So for example inside the file if I have the string "specialstring" then it will replace all of those occurances changing them to "newstring" but also if a file is called "coolfilespecialstring.php" it will change it to coolfilenewstring.php. Also keep capital letters capital letters etc. Oh here is a really good challenge! Perhaps someone knows of some criteria to distinguish between file calls and text. What I actually want to do is change all text say the "specialstring" in the text of files to the "newstring" but leave the file names alone. But the catch is finding some kind of criteria that will only change something that is a text string such as <h1>specialstring</h1> and not a file call in a program such as specialstring.php. That is actually what I would like to do but I don't know what methods would do that.

On top of that I would like to write a perlscript function to do a grep on a specific directory, hopefully to return if and where a searchstring is embedded into html tags say <h1> and </h1>. I however can not get the script to work, there are always errors no matter what I do. Here is an example:

#!/usr/bin/perl

grep -Hrn Searchstring ./Searchfolder

I can get other scripts to work, just not this one and I don't know why. Any ideas? Hows about adding the regex? My biggest concern however is getting the grep shell script to work.

Any ideas?

Thanks,

Brian
 
Use File::Find to find files/directories.
$var =~ /replace/search/g;

Please specifically what you need help with and some code that you are working on.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
$var =~ /replace/search/g;

should be:

$var =~ /search/replace/g;


Look into using perls grep() function or use the system() function to run other programs/applications from within your perl programs.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top