This code uses the %seen hash idiom but resets the hash whenever a new host is reached; this might be tricky if host names are difficult to detect. It replaces empty lines by joining lines to be printed with newlines. Enclosing the selection code in a do block means the %seen hash is not left hanging around.
knoppix@Microknoppix:~$ perl -Mstrict -wE ' > open my $inFH, q{<}, \ <<EOD or die $!; > hostname1.com > > Gateway FAIL > > Gateway FAIL > > Gateway FAIL > > hostname2.com > > Gateway FAIL > > Gateway FAIL > > Gateway FAIL > > EOD > > print join qq{\n}, do { > my %seen; > grep { > %seen = () if m{^hostname}; > ! m{^\s*$} && ! $seen{ $_ } ++; > } > <$inFH>; > }, q{};' hostname1.com Gateway FAIL hostname2.com Gateway FAIL knoppix@Microknoppix:~$
I hope this is of interest.
Cheers,
JohnGG
In reply to Re: Delete Duplicate Entry in a text file
by johngg
in thread Delete Duplicate Entry in a text file
by astronogun
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |