Does anyone have or know of a script that will parse the files, grep the string and replace it with another?
Thank you very much.
Eric Reynolds
#!/usr/bin/perl
$fine = "what_you_want_put";
$replace = "what_to_put_in";
open(FILE,"/path/to/file");
my(@lines) = <FILE>;
close(FILE);
foreach $words (@lines) {
$words =~ s/$fine/$replace/g;
}
Edited by: rcjordan
#!/usr/bin/perl
$fine = "source string";
$replace = "replacement string";
open(FILE2,"> /path/to/outputfile");
open(FILE,"< /path/to/inputfile");
while (<FILE>) {
chomp; #incase moving across file systems linux<->win
s/$fine/$replace/gi;
print FILE2 "$_\n";
}
close(FILE);
close(FILE2); #file is now in file2
btw: this post will eventually move to the technology forum. I love code discussions and hope we can generate enough interest to justify a cgi/perl forum in the near future.
#!/usr/bin/perl
#the path to the files
$serverpath = "/path/to/files/";
#the docs you want changed
$doctype = "txt";
#the string to look for
$fine = "source string";
#what you want to replace it with
$replace = "replacement string";
#
#
#ok, getting started
print "Finding \"$fine\" and replacing it with \"$replace\"\n";
#going to directory
opendir(DIR, "$serverpath");
#looking for files
@files = grep(/\.$doctype$/,readdir(DIR));
closedir(DIR);
#lining the files up
foreach $file(@files){
#opening the file to read it
print "opening file $serverpath$file\n";
open(FILE,"$serverpath/$file");
my(@subm) = <FILE>;
close(FILE);
#clear the document
open(CLR,">$serverpath/$file");
close(CLR);
$n=0;
#printing new
foreach $line (@subm) {
$line =~ s/$fine/$replace/g;
open(SUB,">>$serverpath/$file");
print SUB $line;
close(SUB);
$n++;
}
print "$n lines searched in $file\n";
}
#!/usr/bin/perl
#the path to the files
$serverpath = "/help/needed/here/";
#the docs you want changed
$doctype = "genuine";
#the string to look for
$fine = "learn perl";
#what you want to replace it with
$replace = "no rush";
#
#
#ok, getting started
print "Finding \"$fine\" and replacing it with \"$got books\"\n";
#going to directory
opendir(DIR, "$serverpath"[just need]);
#looking for files
@files = grep(/\.$doctype$/,readdir(DIR));
closedir(DIR);
#lining the files up
foreach $file(@files){
#opening the file to read it
print "opening file $serverpath$file\n";
open(FILE,"$serverpath/$file"[a bit]);
my(@subm) = <FILE>;
close(FILE);
#clear the document
open(CLR,">$serverpath/$file"[of backup]);
close(CLR);
$n=0;
#printing new
foreach $line (@subm) {
$line =~ s/$fine/$replace/g;
open(SUB,">>$serverpath/$file"[any takers]);
print SUB $line;
close(SUB);
$n++;
}
print "$n lines searched in $file\n";
} This might be worth considering. ;)
#!/usr/bin/perl
#the path to the files
$serverpath = "/path/to/files";#no trailing slash
#the docs you want changed
$doctype = "txt";
#the string to look for
$fine = "source string";
#what you want to replace it with
$replace = "replacement string";
#
#
#ok, getting started
print "Finding \"$fine\" and replacing it with \"$replace\"\n";
#going to directory
opendir(DIR, "$serverpath");
#looking for files
@files = grep(/\.$doctype$/,readdir(DIR));
closedir(DIR);
#lining the files up
foreach $file(@files){
$pa_fi = "$serverpath/$file";
#opening the file to read it
print "opening file $pa_fi\n";
open(FILE,"$pa_fi");
my(@subm) = <FILE>;
close(FILE);
#backup the document
rename $pa_fi, $pa_fi . '.bac';
print "making backup\n";
$n=0;
#printing new
foreach $line (@subm) {
$line =~ s/$fine/$replace/g;
open(SUB,">>$pa_fi");
print SUB $line;
close(SUB);
$n++;
}
print "$n lines searched in $file\n";
}
opendir(DIR, "$serverpath");
@files = grep(/\.$doctype$/,readdir(DIR));
closedir(DIR);
There are alot of people who try to hand parse out files through complicated reg ex includes/excludes. The above is sweet, simple, and fast. It could use a dose of pre-error checking to double check that the dir exists, but we'll leave that as an exercise for the reader.