Welcome Guest.   Make a donation to an author on the site April 19, 2014, 01:55:49 AM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
Learn about the DonationCoder.com microdonation system (DonationCredits).
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: Compare text files and keep only the DIFFERENT lines?  (Read 4928 times)
David.P
Supporting Member
**
Posts: 123


Ergonomics Junkie

View Profile Give some DonationCredits to this forum member
« 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
Logged

Crush
Member
**
Posts: 397



Hello dude!

see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« 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 » Logged
housetier
Charter Honorary Member
***
Posts: 1,321


see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #2 on: November 12, 2008, 06:02:32 PM »

Using "diff" would have been my first suggestion also.
Logged
David.P
Supporting Member
**
Posts: 123


Ergonomics Junkie

View Profile Give some DonationCredits to this forum member
« 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
Logged

justice
Supporting Member
**
Posts: 1,885



Solve issues simply.

View Profile WWW Give some DonationCredits to this forum member
« Reply #4 on: November 13, 2008, 03:59:33 AM »

http://gnuwin32.sourcefor...et/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.
Logged

David.P
Supporting Member
**
Posts: 123


Ergonomics Junkie

View Profile Give some DonationCredits to this forum member
« 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
Logged

Merida
Participant
*
Posts: 21


View Profile Give some DonationCredits to this forum member
« Reply #6 on: November 18, 2008, 07:21:42 PM »

+1 for Winmerge
Logged
David.P
Supporting Member
**
Posts: 123


Ergonomics Junkie

View Profile Give some DonationCredits to this forum member
« 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.
Logged

cranioscopical
Friend of the Site
Supporting Member
**
Posts: 4,154



see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« 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.
Logged

Chris
rjbull
Charter Member
***
Posts: 2,701

View Profile Give some DonationCredits to this forum member
« 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.

[copy or print]
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

Logged
David.P
Supporting Member
**
Posts: 123


Ergonomics Junkie

View Profile Give some DonationCredits to this forum member
« Reply #10 on: November 19, 2008, 11:15:44 AM »

Thanks everyone for the great help and suggestions!

Cheers David.P
Logged

housetier
Charter Honorary Member
***
Posts: 1,321


see users location on a map View Profile Give some DonationCredits to this forum member
« 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.
Logged
liuxin4335
Participant
*
Posts: 4


View Profile Give some DonationCredits to this forum member
« 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


I am sales representative of Compare++, any question, you can ask me. Thanks a lot.


* report.jpg (21.08 KB, 400x300 - viewed 146 times.)

* combined_report.jpg (37.19 KB, 400x300 - viewed 138 times.)
« Last Edit: May 30, 2010, 06:27:57 PM by liuxin4335 » Logged
David.P
Supporting Member
**
Posts: 123


Ergonomics Junkie

View Profile Give some DonationCredits to this forum member
« Reply #13 on: May 30, 2010, 06:07:04 AM »

Thanks, looks good!  Cool Thmbsup
Logged

Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.036s | Server load: 0.02 ]