I usually create a new file with its new name rather than renaming the original file and I compare new and old to make sure nothing has gone wrong before unlinking the original. Perhaps an over-complication but a background in systems administration, where trashing a file could bring down a service, has instilled caution.

use strict; use warnings; use POSIX qw{ strftime }; use Digest::MD5; my $origFile = q{cr835.txt}; open my $origFH, q{<}, $origFile or die qq{open: < $origFile: $!\n}; my $line = <$origFH>; close $origFH or die qq{close: < $origFile: $!\n}; my $payeeID; if ( $line =~ m{INC\*XX\*(\d+)} ) { $payeeID = $1; } else { die qq{Could not match payee ID\n}; } my $newFile = qq{cr835-} . $payeeID . strftime q{-processed_%Y-%m-%d_%H-%M.txt}, localtime; open my $newFH, q{>}, $newFile or die qq{open: > $newFile: $!\n}; print $newFH $line; close $newFH or die qq{close: > $newFile: $!\n}; my $origMD5 = Digest::MD5 ->new() ->addfile( do { open my $fh, q{<}, $origFile or die $!; $fh; } ) ->hexdigest(); my $newMD5 = Digest::MD5 ->new() ->addfile( do { open my $fh, q{<}, $newFile or die $!; $fh; } ) ->hexdigest(); if ( $origMD5 eq $newMD5 ) { unlink $origFile or die qq{unlink: $origFile: $!\n}; } else { die qq{Original and new files differ\n}; }

I hope this is helpful.

Cheers,

JohnGG


In reply to Re: Print text on the same line after match by johngg
in thread Print text on the same line after match by periodicalcoder

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.