LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 12: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, 12:36 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Rep: Reputation: 63
Haydar - you do know about diff right?
 
Old 08-02-2008, 07:55 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 08: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, 09: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: 234Reputation: 234Reputation: 234
Please post your final script --
a) It may help someone else.
b) We may have suggestions to improve it.
 
Old 08-02-2008, 11:20 PM   #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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:35 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
Open Source Consulting | Domain Registration