#! perl -slw use strict; use Data::Dump qw[ pp ]; my %hash; while( ) { chomp; my( $pre, $temp ) = m[(^.+?)(\d+|\D)$]; $temp = $pre . ++$temp; if( exists $hash{ $_ } ) { push @{ $hash{ $_ } }, $_; } elsif( exists $hash{ $temp } ) { push @{ $hash { $temp } }, $_; } else { $hash{ $temp } = [ $_ ]; $hash{ $_ } = [ $_ ]; } } print join ';', @{ $_ } for grep{ @{ $_ } == 2 } values %hash; __DATA__ AAA30 BBC5 SHT12H DAL33B BBC49 AAA31 DAL33A BBC6 SHT12G BBC50