LF has asked for the wisdom of the Perl Monks concerning the following question:
s/(?:$search>){3}()\w+\s+/Notes:/; s/(?:$search>){5}()\w+\s+/Director:/; s/(?:$search>){7}()\w+\s+/Actors:/;
my $data = do {local $/;<OLD>}; # slurp the whole file into $data my @replace_list = ('','Notes:','Director:','Actors:'); while ($data =~ s/(?<=$search)/shift @replace_list/egs) {};
Thanks!#!/usr/bin/perl -w use warnings; use strict; my $record_count = 0; my $search = 'dc:description'; my ($dir) = @ARGV; defined($dir) || usage(); chop($dir) if $dir =~ m#/$#; opendir(DIR, $dir) || die "Can't open $dir\n"; $file = readdir(DIR); $file = readdir(DIR); my $temp = 'temp.xml'; while (defined($file = readdir(DIR))) { print "Defined $dir/$file\n"; open(OLD, "< $dir/$file") or die "can't open $dir/$file: $!"; open(TEMP, ">> $dir/$temp") or die "can't open $dir/$temp: $!"; while (<OLD>) { #***insert*** print TEMP $_ or die "can't write $temp: $!"; } close(OLD) or die "can't close $dir/$file: $!"; close(TEMP) or die "can't close $dir/$temp: $!"; rename("$dir/$file", "$dir/$file.orig") or die "can't rename $file to +$file.orig: $!"; rename("$dir/$temp", "$dir/$file"); $record_count++; } closedir (DIR); msg("Processed $record_count files from $dir"); sub msg { print @_, "\n"; } sub usage { msg("Usage: $0 <directory>"); exit(1); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inserting Text into Files within a Directory
by davorg (Chancellor) on Aug 24, 2004 at 14:30 UTC | |
|
Re: Inserting Text into Files within a Directory
by bgreenlee (Friar) on Aug 24, 2004 at 14:47 UTC | |
by LF (Initiate) on Aug 24, 2004 at 15:03 UTC | |
by bgreenlee (Friar) on Aug 24, 2004 at 15:15 UTC | |
by bgreenlee (Friar) on Aug 24, 2004 at 15:34 UTC | |
by LF (Initiate) on Aug 24, 2004 at 16:04 UTC | |
by revdiablo (Prior) on Aug 24, 2004 at 17:05 UTC |