regex - Perl - File Iteration and looking for specific matching data -


i'm looking iterate file in perl , if finds specific word store other lines following matches speific pattern. ldap.txt file pretty large in several gigs.

user.txt

test1   game   

ldap.txt

dn: uid=test1,ou=people,dc=admin,dc=local   blah   blah   maillocaladdress: test1@example.com   maillocaladdress: test.team@example.com   maillocaladdress: test11@example.com   date   more data   data   dn: uid=game,ou=people,dc=admin,dc=local    blah   blah   maillocaladdress: game@example.com    maillocaladdress: game.test@example.com   maillocaladdress: game-test@example.com   date   more data   data   

and on..

open user.txt , iterate through each user , check each line on ldap.txt in dn: line. if matches, store value of lines matching maillocaladdress varialbe , assume in hash key/value pari here values more one.

e.g.

test1 matches dn: uid=test1,ou=people,dc=admin,dc=local   

store following values each user.

test1@example.com   test.team@example.com   test11@example.com   

code

#! /usr/bin/perl  use strict; use warnings;  $ldiffile = shift; %emails;  open $us, '<', 'users2.txt'                   or die "could not open file users2.txt: $!";  open $fh, '<', $ldiffile                  or die "could not open file $ldiffile: $!";  chomp(my @users = <$us>); #print "@users \n";  foreach $uid (@users) { print "$uid \n"; #       while ( chomp(my $line = <$fh>) ) {         while (my $line = <$fh>) {         chomp ($line);                 if ( $line =~ /dn: uid=$uid,ou=people,dc=admin,dc=local/i ) {                 print "$line \n";                         if ( $line =~ /maillocaladdress: ([\w\.\-\_\@]+)/ ) {                                 print "<<<< $line >>>> \n";                                 push ( @{$emails{$uid}}, $1 );                         }                 }         } } 

hash user list. then, iterate on second file. remember user parsing ($user). if see email address, store it.

#!/usr/bin/perl use warnings; use strict;  %users; open $user, '<', 'user.txt' or die $!; while (<$user>) {     s/\s*$//;               #/ sample input contains trailing whitespace.     undef $users{$_}; }  $user = q(); open $ldap, '<', 'ldap.txt' or die $!; while (<$ldap>) {     s/\s*$//;     $user = $1 if /dn: uid=(.*?),ou=people,dc=admin,dc=local/;     push @{ $users{$user} }, $1 if exists $users{$user}                                  , /maillocaladdress: (.*)/; }  $user (keys %users) {     print "$user\n\t";     print join "\n\t", @{ $users{$user} };     print "\n"; } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -