user3019617
user3019617

Reputation: 35

Delete lines with reocurrences

how do I change a text file so it erases only the lines which have the same word?

example:

  1. blue green yellow
  2. red orange brown
  3. violet pink green

desired output

  1. blue green yellow
  2. red orange brown

because of finding the word green, the line was erased

Upvotes: 0

Views: 43

Answers (1)

SzG
SzG

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

Related Questions