#!/usr/bin/perl use strict; use warnings; open(FILE2,">file1.txt")|| warn "Could not open\n"; open(FILE3,"file2.txt")|| warn "Could not open\n"; my $Previous = ""; my @data = ; my $index=0; foreach my $_data (@data) { $index++; chomp ($_data); my @Current = split(/\s+/, $_data); if ($index == 1) { # do nothing. } else { my @Previous = split(/\s+/, $Previous); if ($Current[0] ne $Previous[0]) { print FILE2 $Previous, "\n"; } } $Previous = $_data; } if ($Previous) { print FILE2 $Previous, "\n"; } close(FILE2); close(FILE3);