#!/usr/bin/perl # http://perlmonks.org/?node_id=1208859 use strict; use warnings; my $awsLists = 'd.1208859'; # change to your filename my @commands = ; # 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__ ip-10-1-129-212.us-west-2.compute.internal ip-10-1-129-168.us-west-2.compute.internal ip-10-1-129-212.us-west-2.compute.internal ip-10-1-129-158.us-west-2.compute.internal ip-10-1-129-78.us-west-2.compute.internal ip-10-1-1-145.us-west-2.compute.internal ip-10-1-65-215.us-west-2.compute.internal ip-10-1-129-194.us-west-2.compute.internal ip-10-1-1-252.us-west-2.compute.internal ip-10-1-67-133.us-west-2.compute.internal