使用脚本

时间:2018-09-20 17:53:17

标签: perl

我有一个这样的输出文件,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,您可以看到Positiondn: cn=xxxxxx相同。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这是一种简单的方法。它只是将标题行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";
}