LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-21-2005, 12:56 PM   #1
beep
LQ Newbie
 
Registered: Jun 2004
Location: India
Posts: 6

Rep: Reputation: 0
Using diff for comparing 2 files


I'm quite new to bash scripting (around a week) and have to finish an assignment at the earliest. The question may seem to be simple but i cant find the solution.

I have got two comma separated files which contain name of files and and their versions.

I have to create three text files: 1 for those files which have been added , 1 for those files which have been deleted and 1 for those files whose versions have changed.

I can do it in the classical array and search method (something similar to C) but have to use 'diff' whose output i cant understand.
 
Old 01-21-2005, 01:18 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Ok, I did a diff of two files which are very similar on my machine.

Two seperate lines contain differences.

here is the output of diff

Code:
Quagmire-OSX:/usr/X11R6/lib/pkgconfig root# diff xft.pc xft.pc.2 
1c1
< prefix=/usr/X11R6
---
> prefix=/usr/X11R6/blah
14c14,15
< Libs: -L${libdir} -lXft ${freetypelibs} ${xrenderlibs}
---
> 
> Libs: -L${libdir} -lXft -lX11 ${freetypelibs} ${xrenderlibs}
Ok, This shows two seperate sections of the file which are different.

The first one is labeled 1c1. This means that line 1 has changed from the xft.pc to xft.pc.2. The output after the < shows you what it was in xft.pc and the output after > shows you what it was after xft.pc.2.

The second one is labeled 14c14,15. What this means is diff thinks that in addition to a change on what was line 14 of the first file, there is an extra line as well. Again, the < and > mean the same thing.

Read man diff for more information. I hope this help some.
 
Old 01-21-2005, 01:34 PM   #3
beep
LQ Newbie
 
Registered: Jun 2004
Location: India
Posts: 6

Original Poster
Rep: Reputation: 0
what does diff exactly do?
Does it perform a line by line analysis. If yes then that means two sorted files may give a different reult from two unsorted ones (say f1 is at line1 of file1 and f1 is at line129 of file2 and then if i diff file1 fil2, will it say that the entries are same?)
 
Old 01-21-2005, 01:42 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
If diff detects the file is a text file (like source code) it does a line by line comparison. All I ever use it for personally is to compare source code or config files, and occasionally to see if two binary files are identicle (which it does via a different method then line by line).

Certainly in line by line mode diff would return very different things for a sorted list vs. and unsorted list.

However, diff has a billion options, I'd read the man page if I were you and see if there are any other algorithms it supports to help you with that assignment.
 
Old 01-21-2005, 01:47 PM   #5
beep
LQ Newbie
 
Registered: Jun 2004
Location: India
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by jtshaw
I'd read the man page if I were you and see if there are any other algorithms it supports to help you with that assignment.
I apologise for my ignorance but whats a man page ?
 
Old 01-21-2005, 01:51 PM   #6
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
There is a command in unix like systems called man. It is short for manual. If you type man <command> there will usually be a help file that tells you about the command and all the options it supports.

In case you are using a version of diff for Windows (which doesn't have man) here is a link to the man page. Of course, that only applies if you are using gnu diff (which most people are.. but if your using some wierd non-gnu diff for Windows all bets are off).
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing 2 Files for Duplicates Mr_H Linux - Newbie 5 11-09-2005 01:43 PM
comparing lots of files Frustin Linux - General 4 09-22-2005 03:54 PM
Comparing files contents? hhegab Linux - Newbie 3 07-01-2004 01:45 AM
Comparing 2 Files xianzai Programming 2 05-23-2004 12:50 PM
Comparing files on creation time StarTux Programming 2 08-29-2003 02:08 PM


All times are GMT -5. The time now is 04:42 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 Google+: linuxquestions
Open Source Consulting | Domain Registration