Reputation: 35
how do I change a text file so it erases only the lines which have the same word?
example:
desired output
because of finding the word green, the line was erased
Upvotes: 0
Views: 43
Reputation: 12619
perl -ne 'my $p=1;@w=split;for(@w){$p=0 if $w{$_}}print if $p;$w{$_}=1 for(@w)' file
Trick: I'm using both an array @w
for the words of the current line, and a hash %w
for all words encountered in previous lines. $p
is used to indicate printing is necessary.
This will print foo bar foo
.
A version that doesn't, is even easier, but it's left as an exercise to the OP. :-)
Upvotes: 1