Изменить выходной файл ldap с помощью скрипта - PullRequest
0 голосов
/ 20 сентября 2018

У меня есть такой выходной файл, dn: cn=xxxxxx каждый раз отличается.

Каждый dn: cn=xxxxxx имеет один или несколько Position: cn=xxxxxx

dn: cn=429a0b6b-a311-400c-820f-d4a7bb3274c5,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
Position: cn=307897,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>6866835719</cn><reqdate>1537425657</reqdate><startdate>1537425657</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1537425657</date></change><change><date>1537425811</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537425934</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
dn: cn=80d53356-8159-427a-b98e-32c216e27074,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
Position: cn=4635,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>7733441377</cn><reqdate>1537183136</reqdate><startdate>1537183136</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1537183136</date></change><change><date>1537183470</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537353778</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
Position: cn=5656,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>6587590159</cn><reqdate>1537341752</reqdate><startdate>1537341752</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1537341752</date></change><change><date>1537341808</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537354085</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
Position: cn=5596,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>5399237556</cn><reqdate>1537341373</reqdate><startdate>1537341373</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1537341373</date></change><change><date>1537341531</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537354108</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
dn: cn=89fd0274-7d83-4e19-9725-525e6ac367a3,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
Position: cn=303611,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8039755529</cn><reqdate>1445495634</reqdate><startdate>1445495634</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1445495634</date></change><change><date>1445500976</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537357009</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
Position: cn=303754,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2089908580</cn><reqdate>1445495694</reqdate><startdate>1445495694</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1445495694</date></change><change><date>1445501186</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537357366</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
Position: cn=303612,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>5959003545</cn><reqdate>1445495653</reqdate><startdate>1445495653</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1445495653</date></change><change><date>1445501006</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537357474</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
Position: cn=303613,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>3257198413</cn><reqdate>1445495673</reqdate><startdate>1445495673</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1445495673</date></change><change><date>1445501126</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537357503</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>
dn: cn=4c2506eb-2e19-4e76-b5e1-35aded193872,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
Position: cn=16,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>1122909226</cn><reqdate>1435226618</reqdate><startdate>1435226618</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1435226618</date></change><change><date>1435226619</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537370977</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

И мне нужноэтот файл выглядит так:

dn: cn=429a0b6b-a311-400c-820f-d4a7bb3274c5,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
changetype: modify
delete: delete
Position: cn=307897,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>6866835719</cn><reqdate>1537425657</reqdate><startdate>1537425657</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1537425657</date></change><change><date>1537425811</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537425934</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

dn: cn=80d53356-8159-427a-b98e-32c216e27074,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
changetype: modify
delete: delete
Position: cn=4635,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>7733441377</cn><reqdate>1537183136</reqdate><startdate>1537183136</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1537183136</date></change><change><date>1537183470</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537353778</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

dn: cn=80d53356-8159-427a-b98e-32c216e27074,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
changetype: modify
delete: delete
Position: cn=5656,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>6587590159</cn><reqdate>1537341752</reqdate><startdate>1537341752</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1537341752</date></change><change><date>1537341808</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537354085</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

dn: cn=80d53356-8159-427a-b98e-32c216e27074,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
changetype: modify
delete: delete
Position: cn=5596,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>5399237556</cn><reqdate>1537341373</reqdate><startdate>1537341373</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1537341373</date></change><change><date>1537341531</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537354108</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

dn: cn=89fd0274-7d83-4e19-9725-525e6ac367a3,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
changetype: modify
delete: delete
Position: cn=303611,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>8039755529</cn><reqdate>1445495634</reqdate><startdate>1445495634</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1445495634</date></change><change><date>1445500976</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537357009</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

dn: cn=89fd0274-7d83-4e19-9725-525e6ac367a3,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
changetype: modify
delete: delete
Position: cn=303754,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>2089908580</cn><reqdate>1445495694</reqdate><startdate>1445495694</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1445495694</date></change><change><date>1445501186</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537357366</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

dn: cn=89fd0274-7d83-4e19-9725-525e6ac367a3,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
changetype: modify
delete: delete
Position: cn=303612,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>5959003545</cn><reqdate>1445495653</reqdate><startdate>1445495653</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1445495653</date></change><change><date>1445501006</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537357474</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

dn: cn=89fd0274-7d83-4e19-9725-525e6ac367a3,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
changetype: modify
delete: delete
Position: cn=303613,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>3257198413</cn><reqdate>1445495673</reqdate><startdate>1445495673</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>1</newstatus><date>1445495673</date></change><change><date>1445501126</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537357503</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

dn: cn=4c2506eb-2e19-4e76-b5e1-35aded193872,ou=Named,ou=Identities,ou=Active,ou=Vault,o=acme
changetype: modify
delete: delete
Position: cn=16,ou=Entities,ou=Active,ou=Vault,o=acme#6#<position><cn>1122909226</cn><reqdate>1435226618</reqdate><startdate>1435226618</startdate><enddate>1924902000</enddate><lifecycle><change><previousstatus/><newstatus>0</newstatus><date>1435226618</date></change><change><date>1435226619</date><previousstatus>1</previousstatus><newstatus>3</newstatus></change><change><date>1537370977</date><previousstatus>3</previousstatus><newstatus>6</newstatus></change></lifecycle></position>

Как вы можете видеть для dn: cn=xxxxxx, у которого больше единицы Position, dn: cn=xxxxxx будет таким же.

Любые идеи

1 Ответ

0 голосов
/ 20 сентября 2018

Вот простой подход.Он просто сохраняет строку заголовка dn: cn=xxxxxx в переменной и печатает блок каждый раз, когда видит строку Position::

use feature qw(say);
use strict;
use warnings;

my $fn = 'output.txt';  # The file containing the output

open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!";
my $header; 
while (my $line = <$fh>) {
    chomp $line;
    if ( $line =~ /^dn:\s+cn=/ ) {
        $header = $line;   # <-- store header line in a variable
        next;
    }
    elsif ( $line =~ /^Position:/ ) {
        print_block( $header, $line);
    }
}
close $fh;


sub print_block {
    my ( $header, $line) = @_;

    say $header;
    say "changetype: modify\ndelete: delete";
    say $line, "\n";
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...