Robert Klemme
5/17/2007 11:09:00 AM
On 16.05.2007 17:19, Martin Portman wrote:
> anon1m0us wrote:
>> Hi; I have a command to obtain all the groups a user is part of. The
>> command is:
>> dsquery user domainroot -samid <user name> | dsget user -memberof -
>> expand
>>
>> The output is:
>> "CN=Domain Users,CN=Users,DC=testdc,DC=testroot,DC=test,DC=com"
>> "CN=DA-C3-AB-AB1-SERVER-DATA-CHANGE,OU=ACL_Groups,OU=Groups,OU=ABC,DC=
>> testdc,DC=testroot,DC=test,DC=com"
>>
>>
>> I need to obtain the group name From after CN= until the first comma.
>> I tried scan and match. I can't get it right.
>
>
> irb(main):001:0> output = "CN=Domain
> Users,CN=Users,DC=testdc,DC=testroot,DC=test,DC=com"
>
> irb(main):006:0> output.split(',')
> => ["CN=Domain Users", "CN=Users", "DC=testdc", "DC=testroot",
> "DC=test", "DC=com"]
>
> irb(main):008:0> output.split(',').grep(/CN=/)
> => ["CN=Domain Users", "CN=Users"]
>
> irb(main):009:0> output.split(',').grep(/CN=/).collect{|a| a=~/CN=(.*)/;
> $1}
> => ["Domain Users", "Users"]
>
> You can do what you want with the final array.
>
> The final collect part of the last line is a bit cryptic. Others may
> follow with more elegant ways of doing this.
A variation:
irb(main):018:0* output = "CN=Domain
Users,CN=Users,DC=testdc,DC=testroot,DC=test,DC=com"
=> "CN=Domain Users,CN=Users,DC=testdc,DC=testroot,DC=test,DC=com"
irb(main):019:0> output.split(/,/).map {|s| s[/^CN=(.*)$/, 1]}
=> ["Domain Users", "Users", nil, nil, nil, nil]
irb(main):020:0> output.split(/,/).map {|s| s[/^CN=(.*)$/, 1]}.compact
=> ["Domain Users", "Users"]
irb(main):021:0> output.split(/,/).map! {|s| s[/^CN=(.*)$/, 1]}.compact!
=> ["Domain Users", "Users"]
Or, probably a bit more efficient
irb(main):023:0> output.split(/,/).inject([]) {|ar,s| x=s[/^CN=(.*)$/,
1] and ar << x;ar}
=> ["Domain Users", "Users"]
Or, with enumerator
irb(main):032:0> output.to_enum(:scan, /(?:\A|,)CN=([^,]*)/).map {|m| m[0]}
=> ["Domain Users", "Users"]
A more generic approach would first extract all the information from the
record and then select the piece wanted:
irb(main):037:0> data = output.to_enum(:scan, /(?:\A|,)([^=]+)=([^,]*)/).
irb(main):038:0* inject(Hash.new {|h,k| h[k]=[]}) {|ha,m| ha[m[0]] <<
m[1];ha}
=> {"CN"=>["Domain Users", "Users"], "DC"=>["testdc", "testroot",
"test", "com"]}
irb(main):039:0> data["CN"]
=> ["Domain Users", "Users"]
Ok, I have too much time today... :-)
Kind regards
robert