You might like to consider this example code using inplace edit:

use warnings; use strict; # Create test file open testOut, "> test.txt"; print testOut $_ while (<DATA>); close testOut; # Get "user" edits my @userEdits = ( "Manager 1: IP 127.0.0.1" , "Manager 10: IP 127.0.0.10", "Manager 4: IP 127.0.0.40", "Manager 7:", ); # create update hash from user input my %edits; for (@userEdits) { my ($key, $value) = /(\d+):(?:.*?((?:\d{1,3}\.){3}\d{1,3}))?/; $edits{$key} = [$value, 0]; # value and written flag } # Update the file local $^I = '.bak'; local @ARGV = ('test.txt'); my ($key, $value); while (<>) # inplace edit { ($key, $value) = /(\d+):.*?((?:\d{1,3}\.){3}\d{1,3})/; my $line = $_; if (defined $key && exists $edits{$key}) { next if ! defined $edits{$key}->[0]; #delete file entry $line = "Manager $key: IP $edits{$key}->[0]\n"; ++$edits{$key}->[1]; # Mark as written } print $line; } continue { if (eof) { for (sort keys %edits) { next if ! defined $edits{$_} || ! defined $edits{$_}->[0]; print "Manager $_: IP $edits{$_}->[0]\n" if ! $edits{$_}->[1]; } last; } } __DATA__ Manager 1: IP 127.0.0.1 Manager 3: IP 127.0.0.3 Manager 4: IP 127.0.0.21 Manager 7: IP 127.0.0.12
Manager 1: IP 127.0.0.1 Manager 3: IP 127.0.0.3 Manager 4: IP 127.0.0.40 Manager 10: IP 127.0.0.10

Perl is Huffman encoded by design.

In reply to Re: updating to a file by GrandFather
in thread updating to a file by s_gaurav1091

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.