The trick about using seek and read is that in order to seek to a position from the end of a file, you have to specify a negative number for the offset amount. For example, if the log files for successful runs always have the phrase "Normal termination\n" as the very last thing in each file, that's just 19 bytes you need to read from the end -- but let's pad that a bit, just to be safe:
#!/usr/bin/perl use strict; my $prefix="Kick"; my $Restart="Restart.data"; open( RESTART, $Restart ) or die "Unable to read $Restart: $!\n"; my $AlreadyDone = <RESTART>; my ( $jobs_run ) = ( $AlreadyDone =~ /(\d+)/ ); for ( my $j=0; $j<=$jobs_run; $j++) { my $job_title = sprintf( "%s%04d", $prefix, $j ); if ( open( my $fh, "<", "$job_title.log" )) { seek( $fh, -24, 2 ); read( $fh, my $job_end, 24 ); unlink "$job_title.data" unless ($job_end =~ /Normal/); } else { warn "Unable to read $job_title.log: $!\n"; } }

Some miscellaneous notes:

(updated to add a missing close-paren -- forgot to do "perl -cw" before hitting "create")


In reply to Re^3: Efficient deletion of files / shell interaction by graff
in thread Efficient deletion of files / shell interaction by madd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.