LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 12-04-2007, 07:10 AM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 1,966

Rep: Reputation: 55
Using Diff


How can one compare the difference between 2 files and then redirect them to a file with the changes already applied? In other words compare the changes, make the changes and then write it to a file? thanks
 
Old 12-04-2007, 07:41 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Blog Entries: 1

Rep: Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139
Hi,

You want to make this an automated process? If so: You probably cannot do that.

How would the this automated process know which of the differences are correct?

I.e: File one holds (all on 1 line) 1 2 3 4, and file two holds (again all on one line) 1 2 3 5. Is 4 correct and 5 not correct, is 5 correct and 4 incorrect or are both correct.

Hope I understood correctly and if so I hope this clears things up a bit.
 
Old 12-04-2007, 07:50 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528

Rep: Reputation: 60
Code:
$ cat file_one
this is file_one
hello world

$ cat file_two
this is file_two
hello world

$ diff -u file_one file_two | tee my.patch
--- file_one    2007-12-04 13:47:22.000000000 +0000
+++ file_two    2007-12-04 13:47:11.000000000 +0000
@@ -1,2 +1,2 @@
-this is file_one
+this is file_two
 hello world

$ patch < my.patch
patching file file_one

$ cat file_one
this is file_two
hello world
 
Old 12-04-2007, 07:56 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Blog Entries: 1

Rep: Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139
Hi,

@matthewg42: Ok, that works. But isn't it true that it only works if you know which file is correct and which one isn't?
 
Old 12-04-2007, 08:16 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528

Rep: Reputation: 60
What do you mean?
 
Old 12-04-2007, 08:38 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Blog Entries: 1

Rep: Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139
Hi,

In your example file_one is the file that needs to be changed, file_two is the file that holds the correct data (the 'patch'). In that case your solution works.

If, in the example the OP gave, you have two files which are different, you cannot let an automated process determine which changes are correct (see also post #2).
 
Old 12-04-2007, 10:13 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528

Rep: Reputation: 60
The OP didn't make much sense, and I took a little guess at the intention. It still doesn't make a lot of sense. Perhaps an example would help - can you provide one?
 
Old 12-04-2007, 12:34 PM   #8
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 1,966

Original Poster
Rep: Reputation: 55
Druuna and mathewg42 many thanks, also I love me some Druuna. She is one of favorite Fantasy Art characters.
 
Old 12-04-2007, 12:43 PM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Blog Entries: 1

Rep: Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139
Hi,

I'm still curious what the original problem was. matthewg42 and I both assumed and came up with answers that conflict. Anyway, glad to read that our input helped.

PS: Fantasy Art character??? She's alive in my reality
 
Old 12-04-2007, 06:25 PM   #10
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 1,966

Original Poster
Rep: Reputation: 55
I think that you can just use diff to do everything in one command!
 
Old 12-05-2007, 01:14 AM   #11
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528

Rep: Reputation: 60
No, diff finds differences. Patch apples them (or ed if you tell diff to make ed-style patches).

Besides, if you have both files, and want to change file2 to be like file1, why not just copy file1 over file2?

The whole point of the diff/patch system is that you can send just the diff to someone who has a file which you have, and they can apply it without having to receive a copy of the whole new file (presumably saving bandwidth and time).

Last edited by matthewg42; 12-05-2007 at 01:41 AM.
 
Old 12-05-2007, 05:19 AM   #12
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 1,966

Original Poster
Rep: Reputation: 55
no, what I am mean by my example is to compare two different files (file_one and file_two)for differences and remove the differences and them redirect that to a new single file. I hope I said that correctly.
 
Old 12-05-2007, 05:26 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Blog Entries: 1

Rep: Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139
Hi,

If you want the entries that are both in file_one and file_two and not the differences (in either):

comm -12 file_one file_two > file_three

Example:
Code:
$ cat file_1
1
2
3
4
$ cat file_2
1
2
3
5
$ comm -12 file_1 file_2
1
2
3
The comm command can be more flexible then the diff command. Take a look at both manpages.

Hope this helps.
 
Old 12-05-2007, 05:52 AM   #14
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528

Rep: Reputation: 60
Quote:
Originally Posted by metallica1973 View Post
no, what I am mean by my example is to compare two different files (file_one and file_two)for differences and remove the differences and them redirect that to a new single file. I hope I said that correctly.
OK, even if this is not the command you're looking for, does it essentially do the same thing (this is a test to see if my understanding of the question is correct):
Code:
diff file_one file_two > my.diff
cp file_one file_two
Would you end up with the same results that you want using this method? If not, please try to explain again.
 
Old 12-06-2007, 07:13 AM   #15
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 1,966

Original Poster
Rep: Reputation: 55
PHP Code:
# cat test_data1
192.168.1.1
192.168.2.1
192.168.1.3
192.168.2.2
# cat test_data2
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
# diff test_data1 test_data2 > output
# cat output
2c2
192.168.2.1
---
192.168.1.2
4c4
192.168.2.2
---
192.168.1.4 
This is what I want but without:

PHP Code:
192.168.2.1
192.168.1.2
192.168.2.2
192.168.1.4 
Can diff do this in one command?
 
  


Reply

Tags
diff, ed, patch


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the diff between / and // stardotstar Linux - Newbie 1 09-30-2004 01:02 AM
Dual Boot diff Hard Disk diff OS on Suse 9.1 wilhem Linux - Newbie 1 08-13-2004 06:06 PM
So, What's the diff.? The Manic Mouse Linux - Distributions 3 03-19-2004 07:38 AM
What a Diff! perry General 0 12-05-2003 07:10 PM
what is the diff safrout Linux From Scratch 2 05-27-2002 07:20 AM


All times are GMT -5. The time now is 07:04 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration