#!/usr/bin/perl use strict; use warnings; my $file = '/path/to/file.txt'; my %hash; { #Abschnitstrenner auf "\n\n", siehe perldoc perlvar local $/ = "\n\n"; open my $fh, '<', $file or die $!; while( my $entry = <$fh> ){ my ($cat,$id) = $entry =~ /(cat=\d+).*(ID=[^,]+)/s; push @{ $hash{$id} }, $cat; } } for my $key ( keys %hash ){ print $key, " - " , join ":", @{ $hash{$key} },"\n"; }