#!/usr/bin/perl use warnings; use strict; open my $IN, '<', 'input.txt' or die "Cannot open 'input.txt' because: $!"; open my $OUT, '>', 'output.txt' or die "Cannot open 'output.txt' because: $!"; # Input record separator $/ = '+'; while ( <$IN> ) { # skip empty records next unless /\S/; # remove input record separator chomp; my ( $id, $name ) = ( $1, $2 ) if s/ \s* \^ \s* ( \d+ ) \s* % \s* ( .+? ) \s* \.\| \s* //x; for my $data ( split /\s*;\s*/ ) { print $OUT join( ',', map qq/"$_"/, $id, $name, split /\s*-\s*/, $data ), "\n"; } }