DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

David.P

  • Ergonomics Junkie
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 208
Compare text files and keep only the DIFFERENT lines?
« on: November 12, 2008, 09:00 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

  • Hello dude!
  • Member
  • Joined in 2006
  • **
  • Posts: 402
    • Participated in NANY Challenge Three Times
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #1 on: November 12, 2008, 10:03 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, 10:06 PM by Crush »

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,321
    • Gathering Badge
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #2 on: November 13, 2008, 12:02 AM »
Using "diff" would have been my first suggestion also.

David.P

  • Ergonomics Junkie
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 208
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #3 on: November 13, 2008, 08:00 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

  • Solve issues simply.
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,898
    • Participated in NANY Challenge Four Times
    • Cody Award
    • Coding Snacks
    • DJ Badge - DJ
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #4 on: November 13, 2008, 09:59 AM »
http://gnuwin32.sourceforge.net/packages/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

  • Ergonomics Junkie
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 208
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #5 on: November 13, 2008, 10:29 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
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #6 on: November 19, 2008, 01:21 AM »
+1 for Winmerge

David.P

  • Ergonomics Junkie
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 208
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #7 on: November 19, 2008, 06:43 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,776
    • Cody Award
    • Official Forum Clown
    • rotten turkey sandwich - Rotten Turkey Sandwich
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #8 on: November 19, 2008, 01:45 PM »
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.
Chris

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 3,217
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #9 on: November 19, 2008, 03:08 PM »
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
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

  • Ergonomics Junkie
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 208
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #10 on: November 19, 2008, 05:15 PM »
Thanks everyone for the great help and suggestions!

Cheers David.P

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,321
    • Gathering Badge
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #11 on: November 19, 2008, 11:58 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
Re: Compare text files and keep only the DIFFERENT lines?
« Reply #12 on: May 30, 2010, 10:23 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
[attach]
« Last Edit: May 30, 2010, 11:27 PM by liuxin4335 »

David.P

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

Original address: https://www.donationcoder.com/forum/index.php?topic=15698.0