#!/usr/bin/perl -w use warnings; use strict; my ($tag, %list); while (){ next unless /./; if (/^(\w+)/){ $tag = $1; next; } if (/^-- (\w+)/){ push @{$list{$tag}}, $1; } } for my $type (@{$list{main}}){ if ($list{$type}){ for (@{$list{$type}}){ print "main $type $_\n"; } } else { print "main $type\n"; } } __END__ display -- audit -- bass elucent -- audit -- check main -- display -- elucent -- audit -- check