The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare Files and Output Difference Joesgrrrl UNIX for Dummies Questions & Answers 2 12-13-2008 09:51 PM
Compare two files and output diff to third file altamaha Shell Programming and Scripting 8 09-25-2008 10:42 AM
compare two col from 2 files, and output uniq from file 1 pp56825 Shell Programming and Scripting 2 01-10-2008 11:10 AM
compare two .dat files and if there is any difference pulled into a separate file kirankumar Shell Programming and Scripting 1 04-19-2006 02:13 AM
compare 2 files, output dups to file blt123 UNIX for Dummies Questions & Answers 2 07-15-2004 10:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-20-2009
spartan22 spartan22 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 11
Using grep to compare files and extract the difference to another output file

I have two files with dumps of DNS zones. One old and one new. I need to take the new file to find out what was added to the zone's since the old file was given to me. and output that data into another file so we can import it to our DNS server.

I'm extremely new at Unix and am not sure how to go about doing this...

I attemped to use grep -n -f olddump newdump > output.txt but it's only outputting like two domain names when i know there are hundreds of differences.
  #2 (permalink)  
Old 02-20-2009
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
Check out the diff command.

Code:
man diff
  #3 (permalink)  
Old 02-20-2009
sunadmn sunadmn is offline
Registered User
  
 

Join Date: May 2007
Posts: 4
sdiff will do a side by side compare also if that makes things easier for you.
  #4 (permalink)  
Old 02-20-2009
spartan22 spartan22 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 11
Ok, i gave it a shot with Sdiff. Now is there a way to have Sdiff only display what data actually changed in the output file?
  #5 (permalink)  
Old 02-20-2009
sunadmn sunadmn is offline
Registered User
  
 

Join Date: May 2007
Posts: 4
so what I do is just run it through a grep like this:

sdiff orig_file new_file |grep ">"

That should show you everything that is in the new file and not in the original. You may need to just run the sdiff without the grep to see what I am talking about and you will see how it works.

-Stephen
  #6 (permalink)  
Old 02-20-2009
treesloth treesloth is offline
Registered User
  
 

Join Date: Oct 2008
Location: Orem, Utah
Posts: 72
I did something along these lines not so long ago. From the comm man page:

Quote:
The comm utility reads file1 and file2, which should be sorted lexically,
and produces three text columns as output: lines only in file1; lines
only in file2; and lines in both files.

The following options are available:
-1 Suppress printing of column 1.
-2 Suppress printing of column 2.
-3 Suppress printing of column 3.
It sounds like column 2 is what you're interested in. So, first I sorted my lists:

Code:
sort previouslist > previouslist.sorted
sort newlist > newlist.sorted
Then:

Code:
comm -1 -3 previouslist.sorted newlist.sorted > file.containing.just.new.zones
It seems like comm is not a commonly-used command, so I don't know if there's a lot of variation from one UNIXy system to another. Check your man page.
  #7 (permalink)  
Old 02-23-2009
spartan22 spartan22 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 11
Below is the script i'm using. I chose grep because it's dumping the contents of all the files in the directory into one file while putting the name of the original file next to each entry.

Everything works great until I get to the point of comparing the files and outputting the difference. It seems like it's doing it line for line... for example

www macklaw.com 1.x.x.x ftp macklaw.com 1.x.x.x
ftp macklaw.com 1.x.x.x www macklaw.com 1.x.x.x

If the above data is the way it is output it shows them as being different data, i guess because they aren't on the same line. A few of the lines got swapped around when the new dump was sent out and this is prevalent throughout the file. Is there any clean way to get an exact difference either from two different files, or two directories with a whole listing of other files into one sheet that only list the difference between the two?

Quote:
#
cd new
grep -v -e 'rr.com' -e 'comcastmailservice.com' *.db > ../newzones.txt
cd ..
cd old
grep -v -e 'rr.com' -e 'comcastmailservice.com' *.db > ../oldzones.txt
cd ..
sort newzones.txt > newzonessorted.txt | sort oldzones.txt > oldzonessorted.txt | sdiff oldzonessorted.txt newzonessorted.txt > diff.txt
#
Closed Thread

Bookmarks

Tags
dns, grep, pattern matching files, scripting, unix

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0