remove line from file if more than one pattern appears in different line

75 views Asked by At

I have a file with a patter like this:

1 1 1 2 0 1 0.5
1 2 2 2 0 2 0.5
2 1 1 1 0 1 0.25
2 1 2 2 0 2 0.5
2 3 3 3 0 3 0.25

I want to remove a line if another line in the file has the same last two entries. In the example above this means that line 4 should be removed:

1 1 1 2 0 1 0.5
1 2 2 2 0 2 0.5
2 1 1 1 0 1 0.25
2 3 3 3 0 3 0.25

I can't get my head around how I can do this via the command line or with a simple awk/sed script. Any help is greatly appreciated!

2

There are 2 answers

2
pasaba por aqui On BEST ANSWER

try:

 sort -k 6 f1.txt  | uniq -f 5

assuming the original order of the lines doesn't matters.

0
123 On
awk '!a[$NF,$(NF-1)]++' file

Make an array and check it hasn't already been populated with the last two fields.