in reply to String searching in file

Here's one similar to Re: String searching in file with a couple of extra features.

  1. If file does not exist, that's OK, will make one.
  2. Leaves \n on ip names for ease of handling.
  3. Only writes file if new ips are added.

Note: filename and @commands have to be fixed, I changed them for testing purposes.

#!/usr/bin/perl # use strict; use warnings; my $awsLists = 'd.1208859'; # change to your filename my @commands = <DATA>; # new ips (with \n) my %old; if( open my $in, '<', $awsLists ) # OK if file not there { @old{ <$in> } = (); # note: keys have \n close $in; } my $oldsize = keys %old; @old{ @commands } = (); # add new ips if( keys %old > $oldsize ) # only write if new ips added { open my $out, '>', $awsLists or die "$! opening $awsLists"; print $out sort keys %old; # sort not needed, but nice touch :) close $out; } __DATA__