Question: I am processing large text files (~20MB) containing data delimited by line.
Most data entries are duplicated and I want to remove these duplications to only keep one copy.

Also, to make the problem slightly more complicated, some entries are repeated with an extra bit of info appended. In this case I need to keep the entry containing the extra info and delete the older versions.

e.g.
I need to go from this:

```

BOB 123 1DB
JIM 456 3DB AX
DAVE 789 1DB
BOB 123 1DB
JIM 456 3DB AX
DAVE 789 1DB
BOB 123 1DB EXTRA BITS

```

to this:

```

JIM 456 3DB AX
DAVE 789 1DB
BOB 123 1DB EXTRA BITS

```

NB. the final order doesn't matter.

What is an efficient way to do this?

I can use awk, python or any standard linux command line tool.

Thanks.

Answer: 
How about the following (in Python):

```
prev = None
for line in sorted(open('file')):
  line = line.strip()
  if prev is not None and not line.startswith(prev):
    print prev
  prev = line
if prev is not None:
  print prev

```

If you find memory usage an issue, you can do the sort as a pre-processing step using Unix `sort` (which is [disk-based](http://vkundeti.blogspot.com/2008/03/tech-algorithmic-details-of-unix-sort.html)) and change the script so that it doesn't read the entire file into memory.
If you have perl and want only the last entry to be preserved :

```
cat file.txt | perl -ne 'BEGIN{%k={}} @_ = split(/ /);$kw = shift(@_); $kws{$kw} = "@_"; END{ foreach(sort keys %kws){ print "$_ $kws{$_}";} }' > file.new.txt

```