How do i compare the similarities between two or more text files?

The subject says it all. I am familiar with a number of the diff tools that are available, but I have yet to find a tool or app that will find the similarities between two text files. Any suggestions?

Mac mini, OS X Mavericks (10.9.1)

Posted on Jan 8, 2014 3:18 PM

Reply
12 replies

Jan 8, 2014 3:21 PM in response to globetrotterdk

Get TextWrangler from BareBones software.

It's on the App Store, but the one on the Site will install command line tools also, if you think you might want them. And, I'm not sure if the App Store version can open hidden system files and authenticate to unlock them like the version at the website. Very handy for editing config files.


There's also diff in the Terminal.

Jan 9, 2014 2:12 AM in response to Topher Kessler

From http://hints.macworld.com/article.php?story=20030217061153119


"FileMerge highlights the sections that differ in each file..."


I need to find similarities. I was thinking something along the lines of the similarity-tester package in Ubuntu:

http://unix.stackexchange.com/questions/1079/output-the-common-lines-similaritie s-of-two-text-files-the-opposite-of-diff/94532#94532


Preferably a GUI tool, but command line is OK if I can figure out the proper syntax.

Jan 9, 2014 10:50 AM in response to Linc Davis

That is unfortunately beyond my skill set. What I am trying to do is to compare two files. The one file is a list of all AirPrint compatible printers, while the other is a list of printers that are supported by a specific router model. The idea is to be able to find out which printer models are in both files, but as the lists are relatively long, and I am looking at a number of possible routers, it is usefull to be able to run a number of these comparisons to find similarities.

Jan 9, 2014 11:00 AM in response to globetrotterdk

You can easily automate the comparison of two lists if the entries are in exactly the same form. If both lists are plain text files with entries such as


HP 1000

HP 1001


and so on, OK. Otherwise there's no way to do it that wouldn't be more trouble than it's worth.


A router should be compatible with all AirPrinters, not just some. If it isn't, use another router.

Jan 9, 2014 11:46 AM in response to globetrotterdk

If you are familiar with terminal you can try


diff --unchanged-group-format='%<' --old-group-format='' --new-group-format='' f1 f2


which will print out all identical lines in files f and f2


If you triple click the text to select it all you can then copy and paste it into the terminal. You will need to change f1 and f2 to the names of the files you want to compare.

Jan 10, 2014 2:48 AM in response to Frank Caggiano

Thanks for the reply. I ran the command qnd the text in both files seems to be formatted more or less the same. I assume that is what you mean by "print"? I changed "f1" and "f2" by dragging the rtwo files to the terminal to get the path.


I am not sure what I should do in connection with pasting the two lists into the terminal. It seems to me that I am still lacking the tool or command to compare the similarities between the files. I am at the limit of my skill set here, but wouldn't it be possible to do the following:


run command to find similarities between two files

"cat" the output to a third file.


Just a thought.

Jun 6, 2014 8:10 AM in response to globetrotterdk

just found this discussion, so this answer is a little late.


There's a little known unix (terminal) command called comm.

It compares two files and produces 3 column output:

1 - lines in file1 only

2 - lines in file2 only

3 - lines common to both ! This is what you want.


There is a command line option to suppress any of the 3 columns.

So to print ONLY lines common do file1 and file 2:


comm -12 file1 file2


Hope this helps someone.


PS to BareBones - this would be a cool feature to add to BBEdit. That's what I was looking for when I stumbled on this thread.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do i compare the similarities between two or more text files?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.