Thank you, I have the following which works :
#!/usr/bin/perl use warnings; use strict; my $awsGone = "/ansible/awsGone"; my $awsLists = "/ansible/awsLists"; #open file for appending open(APPEND,'>>',"$awsGone") || die("canot open $awsGone,$!"); #open AWSlIST file for reading open(READ, '<', $awsLists) || die("Could not open file, $!"); #open AWSGONE file for reading open(READGONE, '<', $awsGone) || die("Could not open file, $!"); my @gone = <READGONE>; my @ips; @ips = <READ>; foreach my $addy(@ips) { chomp($addy); #Test if up my $retreval=system qq{nc -w 1 $addy 22 > /dev/null 2>&1}; ##if filesd are good, leave them alone, but if bad put them in $awsGon +e if($retreval==0) { print "This up -> $addy\n"; } else { #check if file is empty, because after ansible runs it + clears this file if(-z "$awsGone") { print "Host not up\n"; print APPEND "$addy\n"; #remove the dead ip from awsLists system qq{sed -i "/$addy/d" "$awsLists"}; } else { ##file is not empty so check if ip is already + in the file my @match = grep{/$addy/}@gone ; if (@match) { print "already exists\n"; system qq{sed -i "/$addy/d" "$awsLists"}; } else { print "Host not up \n"; print APPEND "$addy\n"; system qq{sed -i "/$addy/d" "$awsLists"}; } } } } close APPEND; close READ; close READGONE;

In reply to Re^2: Writing to File based on condition by cbtshare
in thread Writing to File based on condition by cbtshare

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.