#!/usr/bin/perl -w use strict; my %time; my $time = 0; while (<>) { if (!/^\s*#/ and /\.frame (\d+)/) { $time = $1; next if $time{$time}; } push @{$time{$time}}, $_; } for (sort { $a <=> $b } keys %time) { print @{$time{$_}}; }