use strict; use warnings; my ($lastline, $lastevent); while () { chomp; next unless(/EVENT(\d)/); print "$lastline\n" if(defined($lastevent) and $1 ne $lastevent); $lastevent = $1; $lastline = $_; } print "$lastline\n"; __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