What is the difference between the following two commands?
sort -u FILE
sort FILE | uniq
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using
sort -udoes less I/O thansort | uniq, but the end result is the same. In particular, if the file is big enough thatsorthas to create intermediate files, there’s a decent chance thatsort -uwill use slightly fewer or slightly smaller intermediate files as it could eliminate duplicates as it is sorting each set. If the data is highly duplicative, this could be beneficial; if there are few duplicates in fact, it won’t make much difference (definitely a second order performance effect, compared to the first order effect of the pipe).Note that there times when the piping is appropriate. For example:
This sorts the file into order of the number of occurrences of each line in the file, with the most repeated lines appearing last. (It wouldn’t surprise me to find that this combination, which is idiomatic for Unix or POSIX, can be squished into one complex ‘sort’ command with GNU sort.)
There are times when not using the pipe is important. For example:
This sorts the file ‘in situ’; that is, the output file is specified by
-o FILE, and this operation is guaranteed safe (the file is read before being overwritten for output).