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.