#!/usr/bin/perl -w use strict; my %log; foreach() { chomp; if(!/^$/){ /.+(EVENT\d)/; $log{$1} = $_; } } print map { "$log{$_}\n" } sort keys %log; __DATA__ 09:12:50:861 EVENT1 #Don't want this line 09:13:09:467 EVENT1 #Don't want this line 09:13:09:837 EVENT1 09:13:38:059 EVENT2 09:14:03:115 EVENT3 09:14:04:076 EVENT4 09:14:11:376 EVENT1 09:14:34:049 EVENT2 09:14:34:990 EVENT3 #Don't want this line 09:14:34:990 EVENT3 09:14:34:990 EVENT4