[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Parsing output

anon1m0us

5/16/2007 3:04:00 PM

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.

14 Answers

Drew Olson

5/16/2007 3:15:00 PM

0

anon1m0us wrote:
> 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.

I'm not sure I understand you correctly, but is this what you're looking
for:

irb(main):008:0> mystring = "CN=test,blasdhlfkasjdlfjkCN=test1,balkjdf"
=> "CN=test,blasdhlfkasjdlfjkCN=test1,balkjdf"
irb(main):009:0> mystring.scan(/CN=([^,]*),/)
=> [["test"], ["test1"]]

-Drew

--
Posted via http://www.ruby-....

Martin Portman

5/16/2007 3:19:00 PM

0

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.

Martin


Harry Kakueki

5/16/2007 3:25:00 PM

0

On 5/17/07, anon1m0us <anon1m0us@yahoo.com> wrote:
>
> 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.
>
>
>
Is this what you are looking for?

str = "CN=Domain Users,CN=Users,DC=testdc,DC=testroot,DC=test,DC=com"
str =~ /CN=(.*?),/
p $1

Harry

--

A Look into Japanese Ruby List in English
http://www.ka...

Harry Kakueki

5/16/2007 3:36:00 PM

0

On 5/17/07, anon1m0us <anon1m0us@yahoo.com> wrote:
>
> 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.
>
>
>
If you want it at the beginning, this would be better than my first suggestion.

str = "CN=Domain Users,CN=Users,DC=testdc,DC=testroot,DC=test,DC=com"
str =~ /^CN=(.*?),/
p $1

Harry

--

A Look into Japanese Ruby List in English
http://www.ka...

Drew Olson

5/16/2007 3:37:00 PM

0

Drew Olson wrote:
> irb(main):009:0> mystring.scan(/CN=([^,]*),/)
> => [["test"], ["test1"]]

I have a question after studying my own solution for the ruby-wise :)
Why does this scan return an array of arrays rather than a simple array
of matches? I suppose the OP should really do this if he wants the
matches:

mystring.scan(/CN=([^,]*),/).flatten

Why is this flatten necessary? I'm assuming I did something dopey in my
code.

-Drew

--
Posted via http://www.ruby-....

Harry Kakueki

5/16/2007 3:48:00 PM

0

On 5/17/07, Drew Olson <olsonas@gmail.com> wrote:
> Drew Olson wrote:
> > irb(main):009:0> mystring.scan(/CN=([^,]*),/)
> > => [["test"], ["test1"]]
>
> I have a question after studying my own solution for the ruby-wise :)
> Why does this scan return an array of arrays rather than a simple array
> of matches? I suppose the OP should really do this if he wants the
> matches:
>
> mystring.scan(/CN=([^,]*),/).flatten
>
> Why is this flatten necessary? I'm assuming I did something dopey in my
> code.
>
> -Drew
>
> --
> Posted via http://www.ruby-....
>
>
I think it is because of the parentheses. Try removing them and take a look.

Harry

--

A Look into Japanese Ruby List in English
http://www.ka...

Sebastian Hungerecker

5/16/2007 3:58:00 PM

0

Drew Olson wrote:
> I have a question after studying my own solution for the ruby-wise :)
> Why does this scan return an array of arrays rather than a simple array
> of matches?

If you don't use groups within your regexp, scan will just return an array
with the matched strings. If you do however, it will return an array of
arrays, each array containing one string per matched group.
Example:
>> "la=lu,lipp=lapp,slipp=slapp".scan(/\w+=\w+/)
=> ["la=lu", "lipp=lapp", "slipp=slapp"]
>> "la=lu,lipp=lapp,slipp=slapp".scan(/(\w+)=(\w+)/)
=> [["la", "lu"], ["lipp", "lapp"], ["slipp", "slapp"]]


HTH,
Sebastian Hungerecker
--
NP: Dornenreich - Zu Träumen wecke sich, wer kann
Ist so, weil ist so
Bleibt so, weil war so

Drew Olson

5/16/2007 5:51:00 PM

0

Sebastian Hungerecker wrote:
> Drew Olson wrote:
>> I have a question after studying my own solution for the ruby-wise :)
>> Why does this scan return an array of arrays rather than a simple array
>> of matches?
>
> If you don't use groups within your regexp, scan will just return an
> array
> with the matched strings. If you do however, it will return an array of
> arrays, each array containing one string per matched group.
> Example:
>>> "la=lu,lipp=lapp,slipp=slapp".scan(/\w+=\w+/)
> => ["la=lu", "lipp=lapp", "slipp=slapp"]
>>> "la=lu,lipp=lapp,slipp=slapp".scan(/(\w+)=(\w+)/)
> => [["la", "lu"], ["lipp", "lapp"], ["slipp", "slapp"]]
>
>
> HTH,
> Sebastian Hungerecker

Thanks, that makes perfect sense. Incidentally, I did only want the
results of the grouping returned, however now I understand the rational
behind the array of arrays. Very helpful as always.

-Drew

--
Posted via http://www.ruby-....

Robert Klemme

5/17/2007 11:09:00 AM

0

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

chatnoir

2/7/2014 10:36:00 PM

0

On Friday, February 7, 2014 12:58:45 PM UTC-7, El Castor wrote:
> On Thu, 6 Feb 2014 18:13:47 -0800 (PST), chatnoir
>
> <wolfbat359a@mindspring.com> wrote:
>
>
>
> >On Thursday, February 6, 2014 7:04:45 PM UTC-7, David 1950 wrote:
>
> >> Yall See!!!!!
>
> >>
>
> >>  
>
> >>
>
> >> I don told yall hillbillies yall was a bunch of
>
> >> lazy,fat,unmotivated,uneducated LOOOOOSERS!!
>
> >>
>
> >>  
>
> >>
>
> >> so here's proof
>
> >>
>
> >>  
>
> >>
>
> >>  
>
> >>
>
> >> Billionaire Sam Zell, one of the richest people in America, told Betty Liu of Bloomberg that poor people
>
> >> should stop envying the rich and ?emulate them? instead.
>
> >>
>
> >>
>
> >> Zell, who founded Equity Group Investments, was responding to comments made
>
> >> by venture capitalist Tom Perkins, who recently compared the ?demonization? of
>
> >> the wealthy in America to how the Nazis treated the Jews during the Holocaust.
>
> >> ?He?s right,? Zell told Liu. ?The, quote, ?one percent? are being pummeled,
>
> >> because it?s politically convenient to do so.?
>
> >>
>
> >> ?The problem is that the world and this country should not talk about envy
>
> >> of the one percent, they should talk about emulating the one percent,? he
>
> >> continued. ?The 1 percent work harder, the 1 percent are much bigger factors
>
> >> in all forms of our society.?
>
> >>
>
> >>  
>
> >
>
> >Few of the one percenters really worked for what they have!
>
>
>
> They work harder and longer, but more importantly they work smarter.
>
> Men like Ford, Carnegie, Rockefeller, Gates, Jobs, Ellison, and Bezos
>
> have changed the world. Men like you (if that's what you are), just
>
> bitch and moan and try to leach off of others. You enjoy one of the
>
> highest standards of living in the world because of their work, not
>
> yours.

I talking about the rest who inherited their money.