LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   AWK: compare two files (https://www.linuxquestions.org/questions/programming-9/awk-compare-two-files-659877/)

haydar68 08-02-2008 12:07 AM

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

ghostdog74 08-02-2008 12:36 AM

what have you tried so far?

Mr. C. 08-02-2008 02:13 AM

Haydar - you do know about diff right?

pixellany 08-02-2008 07:55 AM

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

haydar68 08-02-2008 08:54 AM

Quote:

Originally Posted by Mr. C. (Post 3234023)
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

archtoad6 08-02-2008 09:38 AM

Please post your final script --
a) It may help someone else.
b) We may have suggestions to improve it.

haydar68 08-02-2008 11:20 PM

Quote:

Originally Posted by archtoad6 (Post 3234321)
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


All times are GMT -5. The time now is 10:48 AM.