script to compare users in files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to compare users in files
# 1  
Old 08-30-2011
script to compare users in files

I have two files with users that I wanted to compare
filea looks like below
Code:
cn=abc
cn=xyz
cn=abc1
cn=abc2

fileb looks like below
Code:
cn=abc
cn=xyz
cn=abc1
cn=abc5

I wanted to write whoever exist in filea and not in fileb to filec
also wanted to write whoever exist in fileb and not in filea to filed

I was using following but it does not give me right output atleast when I compare filea with fileb
Code:
for line in `cat filea`
do
grep $line fileb
if [ $? -eq 0 ];then
echo $line >filec
fi
done

or if there is anyway that we can write a script that can query the ldap directory and run the comparison on line and give me the output.

I know I can use ldapsearch but not sure how I need to execute. The main goal is to compare the ldap directories and give the output whoever exist in ldap directory and not in ldap directory2.

Thanks

Last edited by radoulov; 08-30-2011 at 05:00 PM.. Reason: Code tags.
# 2  
Old 08-30-2011
As far as the shell scripting question is concerned,
you could do something like this:


Code:
4.1.10(4)-release$ head file[ab]
==> filea <==
cn=abc
cn=xyz
cn=abc1
cn=abc2

==> fileb <==
cn=abc
cn=xyz
cn=abc1
cn=abc5
4.1.10(4)-release$ comm -3 <( sort filea ) <( sort fileb )
cn=abc2
        cn=abc5

Assuming your shell supports process substitution.
# 3  
Old 08-31-2011
Thanks. but the comm command does not give me the output file with whatever users exist in file and not in fileb and vice versa. I wanted only missed users in output files so that I can make changes to import back to ldap directory.
# 4  
Old 08-31-2011
Quote:
I wanted to write whoever exist in filea and not in fileb to filec
also wanted to write whoever exist in fileb and not in filea to filed
can you post the sample output you would like to see..my head is spinning trying to decipher the above Smilie, but maybe that is just me from not having a cup of java today :-)
# 5  
Old 08-31-2011
ok. Here is my file A

cn=abc
cn=xyz
cn=abc1
cn=abc2

and file B

cn=abc
cn=xyz
cn=abc1
cn=abc5

In first output file (users_not-exist-fileB.txt) I would like to see
cn=abc2
since that is not exist in file B

In second output file (users_not-exist-fileA.txt) I wanted to see
cn=abc5

as cn=abc5 is not exist in file A.

Hope this helps better.
# 6  
Old 08-31-2011
Will this work for you:
Code:
egrep -xvf File2 File1

# 7  
Old 08-31-2011
using gnu grep.

Code:
$ grep  -v -w -f /tmp/b /tmp/a
cn=abc2


$ grep  -v -w -f /tmp/a /tmp/b
cn=abc5

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to extract/compare from two files.

I have two files : Alpha and Beta. The files are as follows (without arrow marks.) Alpha: A 1 D 90 G 11 B 24 C 15 Beta: B 24 C 0 <-- G 11 D 20 <-- A 4 <-- E 777 <-- Expected output of the script : Alpha: (2 Replies)
Discussion started by: linuxadmin
2 Replies

2. Shell Programming and Scripting

Script to compare two text files

i am working on a shell script and need help in the comparing part of it. for e.g. there two text files like this: file1.txt name1 name2 name3 file1 has to be comared with file2 defaultfile.txt name1 name2 name3 name4 and during comparision with defaultfile.txt if... (2 Replies)
Discussion started by: draghun9
2 Replies

3. UNIX for Dummies Questions & Answers

Unix Script to compare two files

Hello, I have a dat file nctilllist.dat which will be present in the directory path "/usr/lpp/web-data/mfg/nct/file-data/nctilllist.dat" nctillist.dat will have reference to files like DP100001.jpg,DP10002.PDF,DP100003.doc on the path /usr/lpp/web-data/mfg/nct/file-data will have... (12 Replies)
Discussion started by: gayathrivm
12 Replies

4. Shell Programming and Scripting

Shell script to compare two files

I have two files; file A and file B. I need all the entries of file A to be compared with file B line by line. If the entry exists on file B, then save those on file C; if no then save it on file D Note :- all the columns of the lines of file A need to be compared, except the last two columns... (8 Replies)
Discussion started by: ajiwww
8 Replies

5. Shell Programming and Scripting

Perl script to compare two files

hi, As such I am new to perl on google search I found a code for Perl script to compare two files and print differences between them and instead of prinintg I want to store the diff. in a outputfile so can sombody provide assistance upon this from where can I edit in script to store the diff in... (1 Reply)
Discussion started by: dinesh.4126
1 Replies

6. Shell Programming and Scripting

Script to Compare Multiple Files.

Hello Everyone, I have strange situation and unable to think of a solution. Situation is as follow, FolderA contrain following files, File1 File2 File3 File4 I want to compare as follow compare File1 with File2 --> Result Same/Diffrent compare File1 with File3 --> Result... (1 Reply)
Discussion started by: ZalimJin
1 Replies

7. Shell Programming and Scripting

script compare files and backup

Im working on a shell script that uses three parameters, a string to replace, the string replacing, and a file name. In this script it makes a back up file before the replacement occurs. However I want to be able to either search the file to see if it has the string and if it doesnt dont create... (5 Replies)
Discussion started by: gordonheimer
5 Replies

8. UNIX and Linux Applications

How to compare two files using shell script

hi experts please help me to compare two files which are in different directory file1<file will be master file> (/home/rev/mas.txt} ex x1 x2 file2 <will be in different folder> (/home/rev/per/.....) ex x3 x4 the filesinside per folder i need to compare with master file... (1 Reply)
Discussion started by: revenna
1 Replies

9. Shell Programming and Scripting

script to compare files

HI i wil get input from sql query and that too i can get a list o f files or just one. i have to pick up a file from another directory which hads prefix to this prefix.x.x.x.x.x. And we have to discard prefix and use that file name. we have to compare this file name(no need... (0 Replies)
Discussion started by: pulse2india
0 Replies
Login or Register to Ask a Question