if your text file was named test.txt then you could have written a "many"-liner to process it
cat test.txt | perl -ne '($key,$value)=/(servername |serverip: )(.*)/;
+if($key eq "servername "){$data->{$value}=[] unless exists $data->{$k
+ey};$last_servername=$value;};if($key eq "serverip: "){push @{$data->
+{$last_servername}}, $value};END{printf "%s,%s\n",$_,join ",",@{$data
+->{$_}} for keys %$data;}'
you can use the indent unix utility to sort out the code in a readable manner ,but
what the code does is really just takes each line ,sees if its a servername or serverip,if its
a servername that has not been yet encounteret it creates a new arrayref as value of the actual
servername and if its a serverip it checks the last parsed servername and pushes in the appropriate arrayref
the server ip.
at the end, we use the block END to denote the end of the processing of pipe contents
so we now can print out the results,the code for printing can be seen in the END block.
def,456,445566
abc,123
ghi,789