http://www.perlmonks.org?node_id=977514


in reply to Re^4: Delete Duplicate Entry in a text file
in thread Delete Duplicate Entry in a text file

Unless I've misunderstood your re-coding suggestions, they do not produce the OP's desired outcome.

Firstly, I ran that code and got

hostname1.com Gateway FAIL hostname2.com Gateway FAIL
which is exactly what I'd expect. The last line was not duplicated. Perhaps you managed to get some whitespace at the end of that line in your input file?

Secondly, you took out both of the newlines you were printing. Since you are now ignoring the blank lines that are in the input (as I said), you still have to create them on the output. You just don't have to remove the newline from the lines that have non-whitespace characters.

So, to be clear, you should have changed the line

print $line, "\n\n";
to
print $line, "\n";

By the way, these modifications for the blanks are exactly what I was talking about in Re: Delete Duplicate Entry in a text file. To put it together with my code there:

my $last; while (my $line = <$fh>) { next unless $line =~ /\S/; next if defined $last and $line eq $last; print $line, "\n"; $last = $line; }

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^6: Delete Duplicate Entry in a text file
by Kenosis (Priest) on Jun 21, 2012 at 04:09 UTC

    I appreciate your detailed explanation, and hope the OP gets a chance to read your comments. As mentioned, my pedagogical intent was to (slowly) build upon the OP's code. Had I the need, I'd likely do the following:

    my $last = ''; do { say if $_ ne $last; $last = $_ } for grep /\S/, <$fh>;

    Thank you for the dialog on this issue...