script to compare users in files
All,
I have two files with user DN's that exported from two different LDAP directories. I wanted to write a script that reads(checks) users (cn=user1) in file A and check to see if users(cn=user1) exists in file B and give me nice output with what users are missing in file B. I have around 30k users in file A with following format.. Quote:
Anyone have an idea how I can do that with shell script. Thanks |
See this thread to determine how to write a loop in your script and then channel the line you read from file A into a grep command in file B, use the -c to get a count.
http://www.linuxquestions.org/questi...aratly-364259/ To do something this complex, I'd save the user name from file A and the "found count" from file B into an array defined in your script, and then process that array and create an output file using the entries where the found count is zero on a per-user basis. A suggestion is to add: set -x set -v Near the top of your script to output to stdout the flow of the script in order to debug it, and then later comment out those lines as you put the script into use. Further, use functions instead of writing one big, hard to read script. If you aren't familiar with functions in a script, search for some examples, there are plenty. |
Thanks.
I know how that for like do something works. But what I need is for line in filea;do get the "cn=user1" and store in a variable then check fileb to see that variable exists if not write the whole dn to filec. But not sure how I can get the only cn value and store it in a variable and then check to see if that cn value exists in fileb. I have some users like cn=user one,ou=some,o=org Thanks again.. |
Also I just started writing script
Quote:
Quote:
cn=usera cn=userb but I want to get "cn=usera one" I'm not sure if the regular expression works but still testing.. |
Please tell me this is not homework. (No, I'm serious. ;))
Quote:
Code:
awk -v "file1=path-to-file1" -v "file2=path-to-file2" ' I used two identical loops to read in the files. They check if one of the fields is the common name field (cn=), and if so, adds the entire record (as a string) into an associative array keyed by the value of the common name in lower case -- I assume you wish the comparison to be case insensitive. (If not, use $i instead of tolower($i). The sub commands remove the cn= part and any leading and trailing whitespace. Finally, the script loops over all names in cn1, and outputs the ones that are not listed in cn2. Note that unlike normal awk scripts, this one has no input files. It would have been pretty natural to read only the second user list in the BEGIN section, and use a normal rule to process each record in the first file; however, I think you'll probably want to do the check the other way too -- list all users that are listed in the second file but not the first -- and you can only do both if you read both into arrays. So I'm anticipating your needs a bit. I hope this helps, but is not your homework, |
Could we not just use grep or comm?
Code:
#fileA Code:
grep -v -f fileB fileA |
Thanks for your help.
This is NOT home work. I been learning scripting. I'm taking the chances whereever I can write a script in my company. I wrote few sofar. Now I have a requirement that needs to compare couple of files time to time and need to make the changes to the directory based on what we find from comparison. since I'm also learning, I dont wanna use someone else script. I want to write myself so that I can learn and help others in the future. so back to my previous post on 04-01-11, 04:22 PM, I'm not sure why the single line splits into 2-3 or may be more line based on the spaces in between. Quote:
Quote:
Grail - I'm not sure if your solution works bcz dn context is entirely different in two files most of the cases. but will test it when I have similar dn in both files. |
Quote:
into your line variable. I am a little more with the fact that i misread the question that the cn=userX will be what you need to look for but the rest is irrelevant. So how about: Code:
egrep -v $(cut -d',' -f1 fileB | sed ':a N;s/\n/|/;ta') fileA |
All times are GMT -5. The time now is 06:32 PM. |