Okay. Here is an untested modification of the second part of your script that reads the file into an array, applies the regexes to the array and notes if any changes were made. If there were changes, creates an new file and writes the changed content to it, and finally renames the new file over the old file.

Files who do contains anything that needs modifying will be untouched and retain theor original modification timestamps, and the window for errors resulting from system failures is much reduced, though not completely eliminated.

foreach my $file (@files) { open( FILE, '<', $file ) or die "Couldn't open $file: $!"; @data = <FILE>; close( FILE ); my $modified = 0; # Assume no modification foreach (@data) { # Increment the flag if changes are made ++$modified if s/servername\.aa\.company\.zzzz\.com/NEWNAME.\c +om/gi; ++$modified if s/\bservername\.aa\.company\.com\b/NEWNAME\.com +/gi; ++$modified if s/\bservername\b/NEWNAME\.com/gi; } # If we made no modifications, # leave the original file as is if( $modified ) { # Create a new file for the modified data open( FILE, '>', "$file.new" ) or die "Couldn't create $file.n +ew: $!"; print FILE for @data; close(FILE) # Then rename the new file over the old file, # effectively deleting the old # There is still a window of opportunity for error # if the system crashes, but it is much smaller. rename "$file.new", $file; } } print "Total Count = $ct\n";

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: Re: Re: Re: Re: Super Find critic needed by BrowserUk
in thread Super Find critic needed by Anonymous Monk

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.