On 02/10/2013 18:22, Peter Holsberg wrote:
Hi,
I have tried to do this for hours but it looks like it is just too
difficult for me.
I would like a script to open a file in the current directory and edit
it. The file's name is \d{6}.htm
The script then needs to search for a line that begins:
<a class="nolinkunderline"
and delete that line and the one underneath it.
Then then it should append an existing file \d(6).txt (i.e, a file with
the same filename as the
first file but with TXT as its filetype) to the end of the line that is
in its entirety
<pre>
Finally, it should jump to the line
</div></body>
and replace it with
</div>
<script type="text/javascript">
var sc_project=8607440;
var sc_invisible=1;
var sc_security="f3cd5e09";
var scJsHost = (("https:" == document.location.protocol) ?
"https://secure." : "http://www.");
document.write("<sc"+"ript type='text/javascript' src='" +
scJsHost+
"statcounter.com/counter/counter.js'></"+"script>");
</script>
<noscript><div class="statcounter"><a title="web analytics"
href="http://statcounter.com/" target="_blank"><img
class="statcounter"
src="http://c.statcounter.com/8607440/0/f3cd5e09/1/"
alt="web analytics"></a></div></noscript>
</body>
OK here's a working solution. I'm not at all happy about this as you
have made no visible effort of your own.
The original `123456.htm` is copied to `123456 - Copy.htm`, which is
tied to an array using the Tie::File module.
The three changes that I think you intend are made to the array, and it
is then untied to close the file.
Exceptions are raised along the way if there is not exactly one matching
file, or if its contents are not as expected.
HTH,
Rob
use strict;
use warnings;
use Tie::File;
use File::Copy 'copy';
my $dir = '/path/to/my/directory';
opendir my ($dh), $dir;
# Find all files in the directory that match \d{6}\.htm
# Die if none or multiple files found
#
my @files = grep /\A\d{6}\.htm\z/i, readdir $dh;
die 'No matching files found' unless @files;
die 'Multiple matching files found' if @files > 1;
my $file = $files[0];
# Copy the file for safety
# Tie it to an array
#
(my $copy = $file) =~ s/(\.htm)\z/ - Copy$1/i;
copy $file, $copy;
tie my @lines, 'Tie::File', $copy;
my $index;
# Find the first line starting with <a class="nolinkunderline"
# Remove it and the following line
#
($index) = grep $lines[$_] =~ /\A<a class="nolinkunderline"/, 0 .. $#lines;
die 'No class="nolinkunderline" found' unless $index;
splice @lines, $index, 2;
# Find the first line equal to <pre>
# Append the contents of the file with the same name and a .txt type
#
($index) = grep $lines[$_] eq '<pre>', 0 .. $#lines;
die 'No <pre> found' unless $index;
(my $txtfile = $file) =~ s/\.htm/.txt/i;
my $text = do {
open my $fh, '<', $txtfile or die qq{Unable to open "$txtfile": $!};
local $/;
<$fh>;
};
$lines[$index] .= $text;
# Find the first line equal to </div></body>
# Replace it with the contents of the DATA segment
#
($index) = grep $lines[$_] eq '</div></body>', 0 .. $#lines;
die 'No </div></body> found' unless $index;
chomp(my @replace = <DATA>);
splice @lines, $index, 1, @replace;
untie @lines;
__DATA__
</div>
<script type="text/javascript">
var sc_project=8607440;
var sc_invisible=1;
var sc_security="f3cd5e09";
var scJsHost = (("https:" == document.location.protocol) ?
"https://secure." : "http://www.");
document.write("<sc"+"ript type='text/javascript' src='" +
scJsHost+
"statcounter.com/counter/counter.js'></"+"script>");
</script>
<noscript><div class="statcounter"><a title="web analytics"
href="http://statcounter.com/" target="_blank"><img
class="statcounter"
src="http://c.statcounter.com/8607440/0/f3cd5e09/1/"
alt="web analytics"></a></div></noscript>
</body>
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/