Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • September 02, 2015, 07:52:25 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Compare text files and keep only the DIFFERENT lines?  (Read 6356 times)

David.P

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 166
  • Ergonomics Junkie
    • View Profile
    • Donate to Member
Compare text files and keep only the DIFFERENT lines?
« on: November 12, 2008, 03:00:07 PM »
Hi forum,

there's a lot of possibilities to compare text files and keep only the matching lines of both files (you can even do that in MS Word).

However, if I had two text files and wanted to extract the lines that are different in the two files -- how would I go about to do that....?

Thanks heaps already,

Cheers David.P

Crush

  • Member
  • Joined in 2006
  • **
  • Posts: 399
  • Hello dude!
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #1 on: November 12, 2008, 04:03:01 PM »
If you want to code it alone: Create an array of lines for both text files and compare each line with all others. If the line appears again, delete it from the arrays. The result of the lists at the end is the difference that you can append somewhere else.

Pseudo Code:
for each line in a1
  cmp line in a2
   if line exists
     delete a1.line
     delete a2.line

after this do the same again with a2

for each line in a1
  cmp line in a1

or use something like Winmerge or Windiff
   if line exists
     delete a1.line
     delete a2.line
« Last Edit: November 12, 2008, 04:06:04 PM by Crush »

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 1,321
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #2 on: November 12, 2008, 06:02:32 PM »
Using "diff" would have been my first suggestion also.

David.P

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 166
  • Ergonomics Junkie
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #3 on: November 13, 2008, 02:00:49 AM »
Thanks guys,

however I'm not good enough at such coding I'm afraid...

I tried Winmerge but could not find a way to do it there.

Thanks,
David.P

justice

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,896
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #4 on: November 13, 2008, 03:59:33 AM »
http://gnuwin32.sour...ckages/diffutils.htm

You can use the diff command to show differences between two files, or each corresponding file in two directories. diff outputs differences between files line by line in any of several formats, selectable by command line options. This set of differences is often called a `diff' or `patch'. For files that are identical, diff normally produces no output; for binary (non-text) files, diff normally reports only that they are different.

David.P

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 166
  • Ergonomics Junkie
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #5 on: November 13, 2008, 04:29:23 AM »
Thanks! Sounds as if this is the right thing for me -- only a little drawback for users like me that are spoiled by mouse and blinkenlights --> seems as if there is no GUI.

But thank you very much for the tip!

Cheers David.P

Merida

  • Participant
  • Joined in 2008
  • *
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #6 on: November 18, 2008, 07:21:42 PM »
+1 for Winmerge

David.P

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 166
  • Ergonomics Junkie
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #7 on: November 19, 2008, 12:43:51 AM »
Yeah thanks -- but how am I supposed to do that in Winmerge? Have looked around all menus and options but can't seem to find a "select" or "keep only different lines" option.

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,302
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #8 on: November 19, 2008, 07:45:04 AM »
Another, GUI-if-desired alternative might be Beyond Compare.
With it, you could load your files and merge the differences into one, which might satisfy your needs.

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 2,856
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #9 on: November 19, 2008, 09:08:04 AM »
If I understand you (and the comm utility) correctly, I think you might want comm rather than diff.  It's another non-GUI command-line tool ported originally from Unix.

Code: [Select]
Usage: comm [OPTION]... LEFT_FILE RIGHT_FILE
Compare sorted files LEFT_FILE and RIGHT_FILE line by line.

  -1              suppress lines unique to left file
  -2              suppress lines unique to right file
  -3              suppress lines that appear in both files
      --help     display this help and exit
      --version  output version information and exit.

Report bugs to <bug-textutils@gnu.org>.

You'd want the -3 switch, leaving only the lines that aren't common to both files.  Usually, you have to sort the files first for this kind of utility to work as expected.

Available here:  GNU utilities for Win32


David.P

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 166
  • Ergonomics Junkie
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #10 on: November 19, 2008, 11:15:44 AM »
Thanks everyone for the great help and suggestions!

Cheers David.P

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 1,321
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #11 on: November 19, 2008, 05:58:28 PM »
oh wow, I hadn't even heard about comm(1) before.

It reminds me of tac(1) which does the same as cat(1): it prints the contents of a file line by line, but in reverse order.

liuxin4335

  • Participant
  • Joined in 2010
  • *
  • Posts: 4
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #12 on: May 30, 2010, 05:23:12 AM »
You can try Compare++. In the file comparison window, Select File > Report and set "Max numbers of context lines for differences" to 0, you can extract only different lines.
Here is a demo
Compare++_Compare_File.jpg

I am sales representative of Compare++, any question, you can ask me. Thanks a lot.
« Last Edit: May 30, 2010, 06:27:57 PM by liuxin4335 »

David.P

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 166
  • Ergonomics Junkie
    • View Profile
    • Donate to Member
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #13 on: May 30, 2010, 06:07:04 AM »
Thanks, looks good!  8) :Thmbsup: