thank you so much for your help and do very much appreciated
The problem is that my input files are XML and I have to search for multiple lines
Here is my entire code, I have removed many of values from @myHashArray and just left one item
#!/usr/bin/perl ############### ## Libraries ## ############### use File::Copy; use File::Find; use File::Path; use File::Basename; use File::stat; use strict; use warnings; use Time::localtime; ################## ## User-defined ## ################## #--check for rptCustomAssembly word my $find = "CustomAssembly"; #--the root location of files to start reading (this can be our ShareP +oint site) my $rootLocationOfFiles = 'C:\rptTest'; #--the name and location of log file my $logFile = '>C:\rptDest\logRptFindAndReplace.txt'; #--get the start timestamp my $currentTime = time; #--Open a logfile to write the file names open (LOGFILE, $logFile); #--get current timestamp my $startTimeStamp = '[' . timestamp() . ']'; #--write to our logfile print LOGFILE "------------------------------------------------------- +------------\n"; print LOGFILE "Start time: $startTimeStamp\n"; print LOGFILE "Please Note:\n"; print LOGFILE "List of reports that are modified by script.\n"; print LOGFILE "------------------------------------------------------- +------------\n\n"; #----------------------------------------------------- #--Find and replace $source with $target if CustomAssembly is not refe +renced in report file my $source = '<Language>en-US</Language>'; my $target = '<Language>en-US</Language> <CodeModules> <CodeModule>CustomAssembly, Version=1.0.0.1, Culture=neutral, Publ +icKeyToken=null</CodeModule> </CodeModules>'; use File::Find qw(finddepth); my @files; my $counter = 0; my @myHashArray = ( {toFind => '<Image Name="Logo"> <Source>Embedded</Source>', toReplace => '<Image Name="Logo"> <Source>External</Source>' } ); #--END @myHashArray finddepth(sub { return if($_ eq '.' || $_ eq '..'); my ($dir, $name, $ext) = fileparse($File::Find::name,'.rdl'); my $fileName = $_; chop($name); my $fullFileName = "$name\\$fileName"; my $Dest = "$rootLocationOfFiles\\$fileName"; #--read only .rdl files if($ext eq '.rdl') { my $data = read_file($File::Find::name); for ($data) { if ($_ !~ /$find/) { $data =~ s/$source/$target/g; $data =~ s/$_->{toFind}/$_->{toReplace}/g foreach (@myHash +Array); write_file($File::Find::name, $data); #--write to the fi +le print LOGFILE "$counter $File::Find::name\n"; print LOGFILE "\t Original line: HERE WOULD BE MY ORIGINAL + LINE\n"; print LOGFILE "\t Replaced Line: HERE WOULD BE THE NEW LIN +E\n"; print "$counter $File::Find::name\n"; $counter = $counter + 1; #system("copy \"$fullFileName\" \"$Dest\""); #--copy fil +es } else { $counter = $counter + 1; $data =~ s/$_->{toFind}/$_->{toReplace}/g foreach (@myHash +Array); write_file($File::Find::name, $data); print LOGFILE "$counter $File::Find::name\n"; print LOGFILE "\t Original line: HERE WOULD BE MY ORIGINAL + LINE\n"; print LOGFILE "\t Replaced Line: HERE WOULD BE THE NEW LIN +E\n"; print "$counter $File::Find::name\n"; #print "$_->{toFind} : $_->{toReplace}\n" foreach (@myHash +Array); } } } }, $rootLocationOfFiles); my $endTimeStamp = '[' . timestamp() . ']'; my $totalSeconds = time - $currentTime; my $totalMins = ($totalSeconds/60)%60; my $totalSec = $totalSeconds%60; print LOGFILE "------------------------------------------------------- +------------\n"; print LOGFILE "\n"; print LOGFILE "Program ended at: $endTimeStamp\n"; print LOGFILE "It took $totalMins minute(s) and $totalSec seconds\n"; print "DONE, it took $totalMins minute(s) and $totalSec seconds\n"; close (LOGFILE); #----------------------------------------------------- #--Read the file sub read_file { my ($filename) = @_; open my $in, '<:encoding(UTF-8)', $filename or die "Could not open + '$filename' for reading $!"; local $/ = undef; my $all = <$in>; close $in; return $all; } #----------------------------------------------------- #--Write the file back sub write_file { my ($filename, $content) = @_; open my $out, '>:encoding(UTF-8)', $filename or die "Could not ope +n '$filename' for writing $!";; print $out $content; close $out; return; } #----------------------------------------------------- #--get timestamp sub timestamp { my $t = localtime; return sprintf( "%04d-%02d-%02d at %02d:%02d:%02d", $t->year + 1900, $t->mon + 1, $t->mday, $t->hour, $t->min, $t->sec ); } #-----------------------------------------------------
In reply to Re^4: array of hashes help
by rv799cv
in thread array of hashes help
by rv799cv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |