Dave and Phil,

Both of your solutions worked just great.  Thanks for your assitance.

Craig

>>> Philip Andrew <[EMAIL PROTECTED]> 03/04/02 04:23PM >>>
Craig Sharp wrote:

>I am trying to parse a file with the following script:
>
>    .  .  .  .
>
>1. Look at the sample file that is being used for the data.
>   You will see that there are sections that have both
>   employeeNumber and mail and some that have one or the other.
>
>2. I need to have the output as such:
>
>101157       [EMAIL PROTECTED] 
>or
>             [EMAIL PROTECTED] 
>or
>101157
>
>As you can see, if there is an entry without the employeeNumber
>and just the email, I need to print out just the email in the
>second column and vice versa if there is just the employeeNumber
>and no email or if there are both, print both as shown.
>
What I would do is just save the 'employeeNumber' and 'mail' value when 
I encounter them, and then print out 2 columns when I get to the next 
"dn:" statement (and don't forget to do it when you reach end-of-file, 
otherwise you'll miss the last record).

Something like this:

#!/usr/bin/perl
use strict;
open (INFILE, "datafile.txt") || die "$!";
my($employeeNumber,$mail) = ("") x 2;
foreach (<INFILE>) {
  /^employeeNumber:\s*(\d+)/ && do {$employeeNumber=$1; next};
  /^mail:\s*(\S+)/ && do {$mail=$1; next};
  /^dn:/ && ($employeeNumber || $mail) && do {
    printf "%-8s %s\n", $employeeNumber, $mail;
    $employeeNumber = $mail = "";
    next;
  };
}
if ($employeeNumber || $mail) {printf "%-8s %s\n", $employeeNumber, $mail}

-Phil


Full Message:  Craig Sharp wrote:

>Hi all,
>
>I am trying to parse a file with the following script:
>
>#!/usr/bin/perl                                         
>                                                        
>use strict;                                             
>                                                        
>open (INFILE, "datafile.txt") || die "$!";              
>                                                        
>my @lines = <INFILE>;                                   
>                                                        
>my $employeeNumber;                                     
>my $mail;                                               
>my $envalue;                                            
>my $mailvalue;                                          
>                                                        
>foreach (@lines) {                                      
>                                                        
>        chomp;                                          
>                                                        
>        if (/^employeeNumber/) {                        
>           ($employeeNumber,$envalue) = split / /,$_;   
>           print "$employeeNumber $envalue\n";          
>        }                                               
>        elsif (/^mail/) {                               
>           ($mail,$mailvalue) = split / /,$_;           
>           print "$mail $mailvalue\n";                  
>        }                                               
>}                                                       
>
>The print statements are for testing and to see what the program is actually parsing.
>
>I need to pull the employeeNumber and Mail from the file.  This is working fine but 
>there are a few roadblocks that I am hitting.
>
>1. Look at the sample file that is being used for the data.  You will see that there 
>are sections that have both employeeNumber and mail and some that have one or the 
>other.
>
>2. I need to have the output as such:
>
>101157 [EMAIL PROTECTED] 
>or
>             [EMAIL PROTECTED] 
>or
>101157
>
>As you can see, if there is an entry without the employeeNumber and just the email, I 
>need to print out just the email in the second column and vice versa if there is just 
>the employeeNumber and no email or if there are both, print both as shown.
>
>Here is the sample data file:
>
>################### SNIP ####################
>
>dn: Cn:Rjwoll,ou=USR,ou,B008,ou=LIV,o=ROUSH  
>Changetype: add                              
>employeeNumber: 100069                       
>mail: [EMAIL PROTECTED]                    
>                                             
>dn: Cn:Rjwoll,ou=USR,ou,B008,ou=LIV,o=ROUSH  
>                                             
>dn: Cn:Rjwoll,ou=USR,ou,B008,ou=LIV,o=ROUSH  
>                                             
>dn: Cn:Rjwoll,ou=USR,ou,B008,ou=LIV,o=ROUSH  
>                                             
>dn: Cn:mtdean,ou=USR,ou,B008,ou=LIV,o=ROUSH  
>Changetype: add                              
>employeeNumber: 203069                       
>mail: [EMAIL PROTECTED]                    
>                                             
>dn: Cn:cashar,ou=USR,ou,B008,ou=LIV,o=ROUSH  
>Changetype: add                              
>employeeNumber: 101157                       
>                                             
>dn: Cn:Rjwoll,ou=USR,ou,B008,ou=LIV,o=ROUSH  
>Changetype: add                              
>employeeNumber: 000057                       
>                                             
>dn: Cn:cashar,ou=USR,ou,B008,ou=LIV,o=ROUSH  
>Changetype: add                              
>mail: [EMAIL PROTECTED]          
>
>
>Thanks,
>
>Craig          
>
>_______________________________________________
>Perl-Unix-Users mailing list
>[EMAIL PROTECTED] 
>To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs 
>


_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to