LinuxQuestions.org
Help answer threads with 0 replies.
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 08-02-2008, 01:07 AM   #1
haydar68
Member
 
Registered: Jul 2008
Posts: 35

Rep: Reputation: 15
AWK: compare two files


Hi,

I have two files file1 and file2 and want to compare them.
the content of file1 was in the past:
cn=10,ou=work,o=dom.com:vezina:denis:denis.vezina@dom.com
cn=20,ou=work,o=dom.com:saul:jean:jean.saul@dom.com
cn=30,ou=work,o=dom.com:didi:isabelle:isabelle.didi@dom.com
cn=40,ou=work,o=dom.com:kapa:alain:alain.kapa@dom.com

I copy this file in aonther server as file2

I performed some changes on file1, I added :
cn=50,ou=work,o=dom.com:michel:jean:jean.michel@dom.com

I modified the name isabelle by mary in:
cn=30,ou=work,o=dom.com:didi:mary:mary.didi@dom.com

I deleted this line:
cn=40,ou=work,o=dom.com:kapa:alain:alain.kapa@dom.com

Now, the content of file1 is like:
cat file1
cn=10,ou=work,o=dom.com:vezina:denis:denis.vezina@dom.com
cn=20,ou=work,o=dom.com:saul:jean:jean.saul@dom.com
cn=30,ou=work,o=dom.com:didi:mary:mary.didi@dom.com
cn=50,ou=work,o=dom.com:michel:jean:jean.michel@dom.com

But the file2 in the another server is not synchronized, it contains the old data, I have to refresh it:

cat file2
cn=10,ou=work,o=dom.com:vezina:denis:denis.vezina@dom.com
cn=20,ou=work,o=dom.com:saul:jean:jean.saul@dom.com
cn=30,ou=work,o=dom.com:didi:isabelle:isabelle.didi@dom.com
cn=40,ou=work,o=dom.com:kapa:alain:alain.kapa@dom.com

I need to generate 3 files named file.added, file.modified and file.deleted :

File.added should contain :
cn=50,ou=work,o=dom.com:michel:jean:jean.michel@dom.com

file.modified should contain :
cn=30,ou=work,o=dom.com:didi:mary:mary.didi@dom.com

file.deleted should contain :
cn=40,ou=work,o=dom.com:kapa:alain:alain.kapa@dom.com

Is it a way to use awk or sed command to generate these 3 files?

Thanks a lot for your help,

Haydar
 
Old 08-02-2008, 01:36 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
what have you tried so far?
 
Old 08-02-2008, 03:13 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Haydar - you do know about diff right?
 
Old 08-02-2008, 08:55 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Really good tutorials on SED, AWK, and other things: http://www.grymoire.com/Unix

All manner of good things: http://tldp.org
Start with Bash Guide for Beginners, then the Advanced Bash Scripting Guide
 
Old 08-02-2008, 09:54 AM   #5
haydar68
Member
 
Registered: Jul 2008
Posts: 35

Original Poster
Rep: Reputation: 15
Smile

Quote:
Originally Posted by Mr. C. View Post
Haydar - you do know about diff right?
Hi Mr. C.,


You are right, I will use diff to generate thuis output:

diff file1 file2
3,4c3,4
< cn=30,ou=work,o=dom.com:didi:mary:mary.didi@dom.com
< cn=50,ou=work,o=dom.com:michel:jean:jean.michel@dom.com
---
> cn=30,ou=work,o=dom.com:didi:isabelle:isabelle.didi@dom.com
> cn=40,ou=work,o=dom.com:kapa:alain:alain.kapa@dom.com

I will use grep to define what it was deleted or modified or added in file2.
It will avoid me to use other commands where I will use For or While.

Thanks a lot,

Haydar
 
Old 08-02-2008, 10:38 AM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Please post your final script --
a) It may help someone else.
b) We may have suggestions to improve it.
 
Old 08-03-2008, 12:20 AM   #7
haydar68
Member
 
Registered: Jul 2008
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by archtoad6 View Post
Please post your final script --
a) It may help someone else.
b) We may have suggestions to improve it.
Yes of course:
here is the command:

grep -Fvf file1 file2
or
awk 'FNR==NR{tab[$0]++} FNR!=NR && !tab[$0]' file1 file2

the result is the lines located in file2 and not in file1.

grep -Fvf file2 file1
or
awk 'FNR==NR{tab[$0]++} FNR!=NR && !tab[$0]' file2 file1

the result is the lines located in file1 and not in file2.

Then this thread is resolved.

Thanks
 
  


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
Compare files in specific directory to files on CD szim90 Linux - Newbie 3 02-10-2007 09:39 PM
Compare two files namit Linux - Software 1 12-31-2005 09:10 AM
How do I compare 2 files? linuxhippy Slackware 6 03-26-2005 02:54 AM
Diff compare 2 files DavidTempler Linux - Newbie 2 11-01-2004 11:00 AM
to compare the contents of 2 files MaleWithBrains Linux - Newbie 3 01-27-2004 07:39 PM


All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
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