LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   command to compare lines in a file? (https://www.linuxquestions.org/questions/linux-newbie-8/command-to-compare-lines-in-a-file-510996/)

wgato 12-16-2006 04:27 AM

command to compare lines in a file?
 
i want to go through a file, line by line to see if any lines are the same. what command does this?


i'm reading through diff and compare but they seem to be for comparing 2 different files.

thanks!

makyo 12-16-2006 06:16 AM

Hi.

Look at man uniq ... cheers, makyo

titopoquito 12-16-2006 06:41 AM

You could use sort/uniq/diff to find those lines. There are probably simpler solutions :)

Code:

sort textfile.txt > sorted.txt
sort textfile.txt | uniq > sorted-and-uniq.txt
diff sorted.txt sorted-and-uniq.txt

will give you all lines that are duplicates.

linuxone 12-16-2006 12:22 PM

read the man file on uniq, the -d option only prints duplicate lines.

sort textfile.txt|uniq -d

should get you what you want and it's a little more elegant.


Quote:

Originally Posted by titopoquito
You could use sort/uniq/diff to find those lines. There are probably simpler solutions :)

Code:

sort textfile.txt > sorted.txt
sort textfile.txt | uniq > sorted-and-uniq.txt
diff sorted.txt sorted-and-uniq.txt

will give you all lines that are duplicates.


wgato 12-17-2006 08:55 AM

thanks all!
i hadnt heard of uniq before and didnt find it with apropos.


All times are GMT -5. The time now is 09:34 PM.