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 |
what have you tried so far?
|
Haydar - you do know about diff right?
|
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 |
Quote:
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 |
Please post your final script --
a) It may help someone else. b) We may have suggestions to improve it. |
Quote:
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. |