PDA

View Full Version : [SOLVED] Uniq to remove echoed lines



bcooperizcool
July 30th, 2011, 11:34 PM
I have noticed that even when using the correct synatx, uniq does not remove the lines I have echoed into a file.

say, I echo in the following:

echo "Line1" >> file
echo "Line2" >> file
echo "Line1" >> file

so the file contains those lines.

Then, I run it through uniq:

cat file | uniq >> file_no_duplicates

but the contents of file_no_duplicates is still the same. I have tried it multiple different ways, but can't figure it out. Any ideas why?

Thanks! :)

MadCow108
July 30th, 2011, 11:37 PM
uniq only works with sorted files.

Filter adjacent matching lines from INPUT
so do:

sort file| uniq

or shorter for simple cases:

sort -u file

Bachstelze
July 30th, 2011, 11:39 PM
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uniq.html


The uniq utility shall read an input file comparing adjacent lines, and write one copy of each input line on the output.

http://ifaq.wap.org/computers/abcsofunix.html



U is for uniq, which is used after sort, and
V is for vi, which is hard to abort.

bcooperizcool
July 31st, 2011, 12:23 AM
Ohhhhhh..... that would be why. Thank you!