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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how would I search a folder of files for content and make changes

Status
Not open for further replies.

teka2112

Technical User
Feb 5, 2003
14
US
HI

I had good luck with my last question, so I thought id try with another problem im having. and, yes, I do look for the answer in my perl book first - but I cant seem to find what im looking for in there.

so, the problem is:

I have a folder of files - different types (extensions). I need to find all the text files, look in each file for a certain string (same one each time - fixed) and make a very simple change to that string (add just one letter in the correct place - not at the end of the line - in the middle)if it contains the search string (some wont have it) and then save the changes.

I need to write a script for this as there is about 1K files to go through!

thanks for any help here!!
 
Not tested, but you could try something like this:
Code:
use strict;
use warnings;

my $dir = '/home/icrf/goofy/stuff';
my $word = 'cheese';
my $replace = 'beerios';
my $found = 0;

opendir DIR, $dir or die "Unable to open '$dir': $!";
foreach my $filename(readdir DIR)
{
	if($filename =~ /\.txt$/) #your text extention here
	{
		open FILE, &quot;<$dir/$filename&quot; or die &quot;Unable to open '$filename': $!&quot;; #open read
		my @lines = <FILE>; #save file to memory
		close FILE;
		
		for(@lines)
		{
			if(/$word/) #see if you find the word
			{
				$found = 1;
				last; #break out of loop if found
			}
		}
		
		if($found)
		{
			open FILE, &quot;>$dir/$filename&quot; or die &quot;Unable to open '$filename': $!&quot;; #open write
			for(@lines)
			{
				s/$word/$replace/g; #replace word
				print FILE $_; #print back to file
			}
			close FILE;
		}
		$found = 0; #reset found
	}
}
closedir DIR;
$word and $replace is what you're searching for and replacing with, so if it's finding 'ABCD' and replacing just the B with a J, then $replace would be 'AJCD' (replace the whole string, not just the letter).

I can't vouch for efficiency (or even functionality) but it seems to me like something that would work (ie, don't test one irreplacable data first). ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
icrf -

thanks for your help - with just a few tweaks that code worked great! ( and of course when I remembered to chmod my text files properly so it would allow the script to write to them ) :)
 
good to hear ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top