Brad Grandorff am Donnerstag, 14. Dezember 2006 03:02:
> > "D. Bolliger" <[EMAIL PROTECTED]> wrote: Brad Grandorff am Donnerstag, 14.
Dezember 2006 01:58:
> > > I have managed to get a one liner working for modifying a particular
> > > file in a directory, and I have also used arrays to read in then modify
> > > a particular file... but I can't seem to do so using opendir and
> > > reading in several files at once...
> >
> > Could you post your one liner?
> >
> > > Basically I want to search several files within a directory for a
> > > particular keyword, then replace that keyword with a user provided
> > > value..
> > >
> > > I know it is pretty straightforward and simple...
> > > Does anyone have any example scripts / code that I can look at?
> >
> > Did you look at File::Find?
> > "...functions for searching through directory trees doing work on each
> > file found similar to the Unix find command."
> >
> > You could use code from your one liner and modify it for usage in the
> > wanted callback.
>
> Yes I did actually... but I haven't been able to put it all together yet.
> I had originally tried variations of the following..from some example
> code..but it was wiping out the contents of the file when writing.
Hello Brad
(please answer to the list)
> #!/usr/bin/perl
>
> use File::Find;
>
> $path = "/downloads";
> find (\&edits, $path);
>
> sub edits(){
>
> $seen = 0;
>
> if (-f and /.html?/){
# $_ only contains the basename, so the -f test may not test the file you want
# Also, you want to test the file _end_, so you have to anchor the pattern;
# '.' in a regex means "any character" - you want a literal '.'
# See perldoc perlre
> $file = $_;
# replaced by the assignement above
> open FILE, $file;
# Always check if open succeeded
> @lines =<FILE>;
# If you don't have a specific reason to proceed linewise, you could
# slurp the content in a single string, by setting $/ to undef beforehand
# [ local $/; ] (but you seem to have that reason)
> close FILE;
>
> for $line (@lines) {
> if ( $line =~ s/Lesson/Chapter/ ) {
> if ($line =~ s/width/ ) {
# These patterns may not be enough specific, if they should denote words
# and not also word parts.
# The second is a match, not a replacement :-)
> $seen++;
> }
> }
>
> open FILE, ">$file";
> print @lines;
# This will print to the default file handle, STDOUT, not top FILE;
# and, you write the file for every processed line!
> close FILE;
>
> }
> }
>
> print "Found in $File::Find::name\n" if $seen > 0;
>
> }
>
> My one liner I lost...but bascially it simply looked for the following
> regex and replaced it with the user defined variable...:
>
> =~ s/\/sm\/logo.gif/\/sm\/$logo/g;
s|/sm/logo\.gif|/sm/$logo/g; # \/ :-) \/
Ok, here is a version with the modifications mentioned above and some small
others [tested, but afterwards slightly changed]:
#!/usr/bin/perl
use strict; # !!
use warnings; # !!
use File::Find;
my $path = '/downloads';
find (\&edits, $path);
sub edits(){
my $fn=$File::Find::name;
if (-f $fn and /\.html?\Z/){
my $seen = 0;
open my $file, '<', $fn or die $!;
my @lines =<$file>;
close $file or die $!;
my $modified=0;
for (@lines) {
if ( s/Lesson/Chapter/ ) {
$modified++;
$seen++ if /width/;
}
}
if ($modified) {
open $file, '>', $fn or die $!;
print $file @lines;
close $file or die $!;
}
print "'$fn': modifications=$modified, seen=$seen times\n";
}
}
__END__
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>