#!/usr/bin/perl my %cats; my $cat = "_UNDEFINED:"; open my $fh, '<', 'categories.txt' or die; while (<$fh>) { next if m/^\s*$/; #skip blank lines if (m/^(.+:)\s*$/) { $cat = $1; next; } $cats{$cat} .= ">> $_"; } for (sort keys %cats) { print "$_\n"; print $cats{$_}; } close $fh;