#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; my $text = do {local $/;}; $text =~ s/\n(?!\d{7})/ /g; # Remove newline if no # new record my %records = map { split /\s+/, $_ , 2 } split /\n/, $text; print Dumper(\%records) __DATA__ 1000001 01.11.199600.00.00001 A1 1 SN Y 2001.11.200400098.0500073.5500083.35 5001.11.1997Professional attendance being an attendance at 5001.11.1997other than consulting rooms 1000002 01.11.199600.00.00001 A1 1 SN Y 2001.11.200400098.0500073.5500083.35 5001.11.1997Professional attendance being an attendance at 5001.11.1997consulting rooms