Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find unique lines in a text file from command line?

I would like to know how to extract a list of unique lines from a text file. Preferably through Cygwin.

like image 500
Luis Avatar asked Apr 20 '09 21:04

Luis


People also ask

How do I get unique lines in a file?

The uniq command finds the unique lines in a given input ( stdin or a filename command line argument) and either reports or removes the duplicated lines. This command only works with sorted data. Hence, uniq is often used with the sort command. To count how many times each of the lines appears in the file, ...

How do I find unique text files in terminal?

One of the easiest way to get the number of unique words in your file: tr ' ' '\n' < file_name | sort | uniq -c | wc -l.

How do you find unique lines in Unix?

To only show lines that are not repeated pass the -u option to uniq . This will output only lines that are not repeated and write the result to standard output.


2 Answers

Your question is somewhat unclear. If you want to eliminate all duplicate lines from a text file you can do something like this:

cat file.txt | sort | uniq
like image 102
bluebrother Avatar answered Sep 21 '22 16:09

bluebrother


sort -u file > new_file

like image 25
Luis Avatar answered Sep 17 '22 16:09

Luis