Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Matching every instance of text between two characters

by Tuna (Friar)
on Jun 24, 2001 at 01:37 UTC ( [id://91010]=perlquestion: print w/replies, xml ) Need Help??

Tuna has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a script which will return distribution lists, their owners and their members. The file that I am parsing for this info is a .CSV file.

Here's what I've got so far:
#!/usr/bin/perl -w use strict; use Text::CSV; my $file = "/home/trix/Desktop/thisone.CSV"; my ($line, $key1, $key2, $test, $key3, @field, $members, $owner); my $count = 0; my $csv = Text::CSV->new(); open IN, "$file" or die "Can't open $file: $!\n"; while ($line = <IN> ){ chomp $line; $csv->parse($line) ; @field = $csv->fields; $key1 = $field[1]; $key2 = $field[2]; $key3 = $field[3]; ++$count; if ($key1 =~ s/^\\\~//) { print "List: $key1\n"; } if ($key2 =~ /\/o\=NETC\/ou\=(.*)\/cn\=Recipients\/cn\=(.*)/g) { $owner = $2; } else { $owner = "ssesar"; } print "Owner: $owner\n"; ($members) = ($key3 =~ /\=(.*)\%/g); ^^^^^^^^^^^^^^^^ # problematic code print "Members: $members\n\n"; print "############################################\n\n"; } print "Boston: $count lists\n
This will print, for instance:
############################################ List: Prod Dev SP Owner: NONE Members: Boston/cn=Recipients/cn=jdye%/ou=San Francisco/cn=Recipients +/cn=onisbett%/ou=Boston/cn=Recipients/cn=acorneau%/ou=Boston/cn=Recip +ients/cn=kfleming%/ou=Boston/cn=Recipients/cn=amiller%/ou=Boston/cn=R +ecipients/cn=rcrow%/ou=Boston/cn=Recipients/cn=tgoshco%/ou=San Franci +sco/cn=Recipients/cn=ldixon ############################################ List: Prod Dev IT+CR+ME Owner: NONE Members: San Francisco/cn=Recipients/cn=medelstein%/ou=Boston/cn=Reci +pients/cn=thodes%/ou=San Francisco/cn=Recipients/cn=gdistasi%/ou=Bost +on/cn=Recipients/cn=staylor%/ou=Boston/cn=Recipients/cn=drico%/ou=Bos +ton/cn=Recipients/cn=kkarrman%/ou=Boston/cn=Recipients/cn=tgoshco%/ou +=San Francisco/cn=Recipients/cn=rscaife ############################################
My question is, how can I extract only the member's names from the string of data after Members:?

It seems to me that the way to approach this problem is to match everything between "=" and "%". Or, maybe there's a better way to parse CSV data??

If it helps, the CSV file that I am using can be found here.

Replies are listed 'Best First'.
Re: Matching every instance of text between two characters
by bikeNomad (Priest) on Jun 24, 2001 at 03:08 UTC
    If every member has the form  /ou=Boston/cn=Recipients/cn=thodes then you could do something like this:
    #!/usr/bin/perl -w use strict; while (<DATA>) { chomp; my $separator = qr{(?:%?/ou=)|/cn=}; my @fields = split( $separator, $_ ); shift(@fields); # remove empty first field while (my ($city, $recip, $name) = splice(@fields, 0, 3)) { die "bad $recip format\n" if $recip ne 'Recipients'; print "city: $city name: $name\n"; } } __DATA__ /ou=Boston/cn=Recipients/cn=jdye%/ou=San Francisco/cn=Recipients/cn=on +isbett%/ou=Boston/cn=Recipients/cn=acorneau%/ou=Boston/cn=Recipients/ +cn=kfleming%/ou=Boston/cn=Recipients/cn=amiller%/ou=Boston/cn=Recipie +nts/cn=rcrow%/ou=Boston/cn=Recipients/cn=tgoshco%/ou=San Francisco/cn +=Recipients/cn=ldixon /ou=San Francisco/cn=Recipients/cn=medelstein%/ou=Boston/cn=Recipients +/cn=thodes%/ou=San Francisco/cn=Recipients/cn=gdistasi%/ou=Boston/cn= +Recipients/cn=staylor%/ou=Boston/cn=Recipients/cn=drico%/ou=Boston/cn +=Recipients/cn=kkarrman%/ou=Boston/cn=Recipients/cn=tgoshco%/ou=San F +rancisco/cn=Recipients/cn=rscaife
Re: Matching every instance of text between two characters
by toma (Vicar) on Jun 24, 2001 at 06:23 UTC
    Since you are working on LDAP data, you might want to consider using one of the LDAP modules, such as DBD::LDAP. This module adds a database-independent SQL layer to an LDAP database. It provides an alternative to dealing with your data as comma-separated values.

    It should work perfectly the first time! - toma

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://91010]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 22:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found