open FH,"<d:/a.txt"; while(<FH>){ if(m/^1.*/){ $line=$_; print; next; } if(m/^6.*/){ @t=split; if($temp eq ""){ $temp=$t[3]; print; } else{ if(!($t[3] eq $temp)){ print $line; print $_; $temp=$t[3]; } else{ print; } } } else{print;} }