Forum Moderators: coopster & phranque

Message Too Old, No Replies

Regular Expressions

grep and replace

         

Reynolds

2:53 am on Sep 22, 2000 (gmt 0)

10+ Year Member



Hello
This may be the wrong forum but I know a lot of you are very sharp so I'm going to try.
I have a bunch of .txt files. Each file has a common string of text.

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

oilman

4:20 am on Sep 22, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to the forums Reynolds. Sorry I can't answer your question but I know a number of folks around here can do that stuff in their sleep. ;)

Guys...little help?

littleman

4:49 am on Sep 22, 2000 (gmt 0)



At the simplest level you would do something like this:

#!/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;
}

So what do you want to do with it then? Print it, or replace the file with the altered one?

DaveAtIFG

6:26 am on Sep 22, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A handy-dandy quick reference on regular expressions is at [brainstorm.co.uk...]
It may help with LM's variables "what_you_want_put" and "what_to_put_in".
[marker rcjordan]

Edited by: rcjordan

littleman

7:51 am on Sep 22, 2000 (gmt 0)



Dave, nice link - it is kind of like a handy reference sheet.

Reynolds

11:15 am on Sep 22, 2000 (gmt 0)

10+ Year Member



Thank you very much everyone.

Brett_Tabke

11:49 am on Sep 22, 2000 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



And keeping with the net geek perl creed of nitpicking any perl post (lm understands) here is a version that will work with larger files. Takes the input file and outputs it to the output file.

#!/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.

littleman

7:06 pm on Sep 22, 2000 (gmt 0)



Ok, here is more for you to nitpick. I wasn't sure which way we were going to go with this, but it looks now like we are talking about getting a file and altering it - as
oppose to printing it. So I threw this together:

#!/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";
}


It will search and replace an entire folder for an extension and then will do a search and replace for all the docs.

NFFC

7:27 pm on Sep 22, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm...


#!/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. ;)

littleman

8:19 pm on Sep 22, 2000 (gmt 0)



Ok

#!/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";
}

Brett_Tabke

12:21 pm on Sep 25, 2000 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Ok, lets go the other way this time instead of nitpicking, lets point out some of the better lines. The best snip of the whole thread is this one:

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.

sugarkane

7:57 pm on Sep 26, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick and dirty version if you have access to perl from the shell:

perl -e 's/old string/new string/' -p -i.bak *.txt

littleman

5:54 am on Sep 27, 2000 (gmt 0)



sugarkane
Nice one-liner! Anyone know of a website that specializes in command line tricks?