Ubuntu – Deleting duplicate lines in text file…

command linetext processing

How can I delete duplicate lines in a text file via command prompt?

For Example:
I have a 10MB text file and I want to keep only one line of My line, but somewhere in the text file there are 2 My lines.

Best Answer

Using awk

awk '!x[$0]++' infile.txt > outfile.txt

the way it works is that it keeps count of the lines in an array, and if the current count is zero, ie the first occurance, it prints the line, otherwise it continues to the next one.