On Fri, Nov 13, 2009 at 5:07 AM, Parag Kalra <paragka...@gmail.com> wrote:
> Hey All, > > I have a following a LDIF file to process through Perl - > > ############################################################### > dn: ou=71404558, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=People, ou=71404558, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=Groups, ou=71404558, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=40208723, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=People, ou=40208723, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=Groups, ou=40208723, ou=Company, ou=Personal, o=paragkalra.com > dn: cn=Mygroup,ou=Groups, ou=40208723, ou=Company, ou=Personal, o= > paragkalra.com > dn: ou=63613012, ou=Company, ou=Personal, o=paragkalra.com > dn: cn=Manager,ou=63613012, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=People, ou=63613012, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=Groups, ou=63613012, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=$%^&?()+-, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=[]...@#%*, ou=Company, ou=Personal, o=paragkalra.com > dn: cn=CEO,ou=[]...@#%*, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=76884580, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=People, ou=76884580, ou=Company, ou=Personal, o=paragkalra.com > dn: ou=Groups, ou=76884580, ou=Company, ou=Personal, o=paragkalra.com > ################################################################ > > My aim is to extract all those lines starting with the string - 'dn:ou' and > having only 3 levels of OU heirarchy i.e having the string 'ou=' only 3 > times. Also he value of 'ou' can be anything i.e any character (including > special characters) except comman (,). > > So to accomplish it I have written following code and is working - > > ################################################################# > # Author: Parag > > my $file = $ARGV[0]; > > open FILE, $file; > > while ( <FILE> ) { > $count = 0; > $_ =~ s/(\s)+//g; > $count++ while $_ =~ /(ou=)/g; > if ( ($count == 3) && ($_ =~ /^(dn:ou)/) ) { > print "$_\n"; > } > } > > close FILE; > > ################################################################ > > > > But I would like to accomplish it through 1 singe regex like using > following > code - > > > ############################################################ > # Author: Parag > > my $file = $ARGV[0]; > > open FILE, $file; > > while ( <FILE> ) { > $_ =~ s/(\s)+//g; > if ( $_ =~ /(dn:ou=)([\d\D]+)(ou=Company,ou=Personal,o=paragkalra.com > )/){ > print "$_\n"; > } > } > > close FILE; > > ############################################################ > Any pointers? > Nope. Just trial and error until you come up with an ugly hack. I think this works: use strict; use warnings; use 5.010; open (my $INFILE, '<', shift @ARGV); while (<$INFILE>) { print if /^dn: ((ou=[^,]+\s*,)\s*){3}[^,]*$/; } close $INFILE;