#! /usr/bin/perl -w use strict; use vars qw ( $string_in_file $delete_candidate @data_out $data_out ); foreach ( ) { $string_in_file = $_; # get the latest line from the file to the var if ( $string_in_file =~ /^(\*{4}200[^6].*)$/ ) { # Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE $delete_candidate = $1; s/$delete_candidate//; } else { push @data_out, $string_in_file; } } for $data_out(@data_out) { print $data_out; } __DATA__ ****2000-01-01****test1*****12345***** ****2001-01-01****test1*****12345***** ****2002-01-01****test1*****12345***** ****2003-01-01****test1*****12345***** ****2004-11-19****test1*****12345***** ****2005-07-01****test1*****12345***** ****2006-05-09****test1*****12345*****