in reply to building an ldif from 2 files

You'd endear yourself more if you followed the (simple) local formatting conventions.

Lots of room for error checking, but ought to work. (Note: This writes the file directly. No redirection required.)

#! perl -slw use strict; use constant batchdir => "e:\meta"; use constant { infile => batchdir . "\DDNs3"; nfile2 => batchdir . "\DDNsUid2"; outfile => "DDNs3.ldif", ## Assume outfile n current dir per .cmd }; unlink outfile; ## If there delete it; silently do nothing if not. open DN, '<', infile or die $!; open UID, '<', infile2 or die $!; open OUT, '>', outfile or die $!; while( <DN> ) { chomp; print OUT "dn: $_"; print OUT "changetype: notify"; print OUT "replace: employeeNumber"; chomp( my $empNo = <UID> ); print OUT "employeeNumber: $empNo"; } close OUT; close UID, close DN; __END__ @echo on ::Set BATCH Input Directory set batchdir=e:\meta ::Set the input file containing the list set infile=%batchdir%\DDNs3 set infile2=%batchdir%\DDNsUid2 ::If exists, we remove output file rm DDNs3.ldif ::For loop below process each line in the input list. :uid FOR /F "tokens=* delims=" %%i IN (%infile%) do ( echo dn: %%i echo changetype: modify echo replace: employeeNumber call :loop2 ) goto :eof :loop2 FOR /F "tokens=* delims=" %%k IN (%infile2%) do ( echo employeeNumber: %%k echo. call :uid ) :eof

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: building an ldif from 2 files
by rfransix (Novice) on May 26, 2010 at 16:44 UTC
    I fixed this by changing the code (see below). However, I need an empty line after the employeeNumber: 123456789 line, how is the "print OUT...constructed? I tried print OUT; print OUT ""; print OUT \n; Ideas? #! perl -slw use strict; use warnings; use constant batchdir => "e:\meta"; open DN, "<DDNs3" or die $!; open UID, "<DDNsUid2" or die $!; open OUT, ">DDNs3.ldif" or die $!; while( <DN> ) { chomp; print OUT "dn: $_"; print OUT "changetype: modify"; print OUT "replace: employeeNumber"; chomp( my $empNo = <UID> ); print OUT "employeeNumber: $empNo"; print OUT; }; close OUT; close UID; close DN;

      Yes. The code is untested. Switch all '\'s in the path names to '/'s and Perl will do the right thing. Also, I just noticed a typo: nfile2 should be infile2.

      There may well be other bugs or typos you'll need to fix. There idea here is for use to help you (learn Perl), not just give you solutions, so you may have to put a little effort in :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.