On Oct 2, 2013, at 10:22 AM, 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

That is an unusual name for a file. I would rename it.

open(my $f1, ',', '\d{6}') or die("Can't open input file: $!");

Also open a new file:
open(my $out, '>', 'new.htm') or die("Can't open output file: $!");

> 
> The script then needs to search for a line that begins:
> 
> <a class="nolinkunderline"

while( my $line = <$f1> ) {
  if( $line =~ /^<a class="nolinkunderline"/ ) {
    # don't write out this line
    # skip next line:
    <$f1>;
  }else{
    # write out line
    print $out $line;
  }
}


> 
> 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>

Your first file name uses braces ({}), while your second file name uses 
parentheses (()). Your unusual file names have led to confusion. 

Do you mean insert the second file to the end of the line "<pre>" in the first 
file?

If so, then before you read the first file, open and read the contents of the 
second file into a variable. Then, add a test for the line <pre>\n" in the 
while loop above. If found, write the contents of the saved variable to the 
output file.

> 
> 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>

Add a test for "<div><\body>\n" in the while loop. When found, write the above 
lines to the output file.

> 
> Too hard for me. Sorry.

It's not that hard. Just break the problem down into pieces:

1. Read the first file (\d{6}.htm) and look for your special lines

2. Read the second file and save its contents.

3. Write the output file.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to