Something like this:
#!/usr/bin/perl
use warnings;
use strict;
my %data_needed;
my $key;
while (<DATA>) {
chomp;
if (/^(\d.+?)(\d{8,}.+?)?$/) {
$key = $1;
push @{ $data_needed{$key} }, $2;
}
else {
s/^\s+//;
push @{ $data_needed{$key} }, $_;
}
}
{
no warnings 'uninitialized';
for ( keys %data_needed ) {
my $space = " " x length($_);
my $value = join "\n$space", @{ $data_needed{$_} };
printf "%s%s\n\n", $_, $value
if $value =~ /---/ or $value eq '';
}
}
__DATA__
7 hostname12 Generic-legacy 10000000AB210ACF6 ---
10000000AB210ACF4 2:5:4
10000000AB210ACF4 2:3:4
10000000AB210ACF6 3:5:4
9 hostname13 Generic 10000000AB2A3006A 3:5:2
10000000AB2A30068 2:5:2
20 hostname14 Generic-legacy 10000000AB2A3000C ---
10000000AB2A3000E 3:3:1
21 HOSTNAME Generic
22 hsname12 Generic-legacy 10000000ABCDE004A 3:3:3
10000000ABCDE004A 3:5:2
10000000ABCDE0048 2:3:3
23 srvernam Generic-legacy 5001438002A3004A 3:3:3
5001438002A3004A ---
5001438002A30048 2:3:3
5001438002A30048 2:5:2
5001438002A30048 2:5:2
Output:
23 srvernam Generic-legacy 5001438002A3004A 3:3:3
5001438002A3004A ---
5001438002A30048 2:3:3
5001438002A30048 2:5:2
5001438002A30048 2:5:2
21 HOSTNAME Generic
7 hostname12 Generic-legacy 10000000AB210ACF6 ---
10000000AB210ACF4 2:5:4
10000000AB210ACF4 2:3:4
10000000AB210ACF6 3:5:4
20 hostname14 Generic-legacy 10000000AB2A3000C ---
10000000AB2A3000E 3:3:1
Note:
Please, note that the order of your output might be different to what I have here. Of course, the
desired display
order is left to the OP.
Hope this helps
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author
unknown to me