Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

building an ldif from 2 files

by rfransix (Novice)
on May 25, 2010 at 20:04 UTC ( #841628=perlquestion: print w/ replies, xml ) Need Help??
rfransix has asked for the wisdom of the Perl Monks concerning the following question:

I'm praying the monks will see through the windows command logic and see a great script that will properly build a changetype modify ldif file from data in 2 different files.

Here's the code that does not work to build an ldif import file. I have 2 files, line for line they match up. One file has all the DN's, the other file has the employeeNumber of each DN. I can't get the two FOR loops to iterate through 2 files one line at a time to build the correct ldif entry, and then proceed through both files iteratively until the last lines.

@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

20100528 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Comment on building an ldif from 2 files
Download Code
Re: building an ldif from 2 files
by ack (Deacon) on May 25, 2010 at 21:14 UTC

    I will try to take a look at this a little later (unless others respond with whatever my suggestions would be).

    But please note that it would be most helpful if you would use code tags so that we don't have to slog through you're code embedded in normal text (trying to do so is both really annoying and prone to error on the readers' part). Look in the "Tutorials" section of this site to see how to properly format code for the Monestary to look at.

    You sound like you have an interesting (at least to me) challenge to tackle and I am anxious to dig into it. I know that we have many esteemed Monks in the Monestary that can assist...most much better and more qualified that I. I hate to see you miss out on the help just because of some formatting faus pax.

    Good luck.

    ack Albuquerque, NM
Re: building an ldif from 2 files
by NetWallah (Abbot) on May 25, 2010 at 21:17 UTC
Re: building an ldif from 2 files
by tokpela (Chaplain) on May 25, 2010 at 21:18 UTC

    Have you looked into Text::Diff?

    Adapted from the documentation sample:

    use strict; use warnings; use Text::Diff; my $diff = diff "file1.txt", "file2.txt", { STYLE => "Context" }; print "$diff\n";

Re: building an ldif from 2 files
by BrowserUk (Pope) on May 25, 2010 at 21:28 UTC

    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.
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://841628]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2014-10-22 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls