Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Perl question about manipulation of LDIF file

by Anand1974 (Initiate)
on Oct 10, 2017 at 08:19 UTC ( [id://1201076]=perlquestion: print w/replies, xml ) Need Help??

Anand1974 has asked for the wisdom of the Perl Monks concerning the following question:

My challange:

I have got a (ldif) file with a lot of entries. Some of them needs to be reformatted.
The rule for when to reform something is like this:

If a line is equal to "-" then check the previous two lines. If they both begin with a space (" ") then the space in the 2nd line should be removed and the line should be appended to the first line (the line break should be removed).

Example:
Test123 Blah -
should be transformed into:
Test123Blah -

Replies are listed 'Best First'.
Re: Perl question about manipulation of LDIF file
by haukex (Archbishop) on Oct 10, 2017 at 08:34 UTC

    It's probably best to use an appropriate module for this. Net::LDAP::LDIF appears to normalize the entries. I haven't used perl-ldap much myself but as far as I know it's a very good module for LDAP.

    use warnings; use strict; use Net::LDAP::LDIF; my $ldif = Net::LDAP::LDIF->new("test.ldif", "r", onerror=>'die'); while ( not $ldif->eof ) { my $entry = $ldif->read_entry; print $entry->ldif; } $ldif->done;

    Given the following input, the last three lines before the dash are transformed into one line "foo: BarTest123Blah", which I guess is what you want?

    dn: cn=Test,ou=Foo,o=Bar,c=US changetype: modify replace: foo foo: Bar Test123 Blah -
Re: Perl question about manipulation of LDIF file (sliding window)
by LanX (Saint) on Oct 10, 2017 at 08:43 UTC
    TIMTOWTDI

    You may want to have a look at this post Re^2: Grab 3 lines before and 2 after each regex hit (sliding window).

    The update shows a generic solution how to parse a sliding window of 6 lines and test the 4th one.

    use strict; use warnings; use Data::Dump; my @window; while ( my $line = <DATA> ) { push @window, $line; next if @window < 6; # init if( $window[3] =~ m/[^\d]+\d+/ ){ dd \@window; } shift @window; }

    You could easily adapt this to your needs.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: Perl question about manipulation of LDIF file
by Corion (Patriarch) on Oct 10, 2017 at 08:24 UTC

    What code have you already written and where does it fail to do what you need?

    Personally, I would read the file line-by-line and accumulate all data into an array until I encounter a line with only a dash on it. Then I would look at the three last lines in the array and reformat them if needed. After that, I would output and reset the array.

Re: Perl question about manipulation of LDIF file
by tybalt89 (Monsignor) on Oct 10, 2017 at 15:25 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1201076 use strict; use warnings; print +(join '', <DATA>) =~ s/^ .*\K\n (?=.*\n-)//gmr; __DATA__ a data line Test123 Blah - something else do not change this line - a data line combine th is line - something else
      I am impressed by your suggestion tybalt89! It works wonders. Thank you so much!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found