#!/usr/bin/perl -l # http://perlmonks.org/?node_id=1169780 use strict; use warnings; my @allele = ; my @newcounts; $_ = join '', @allele; push @newcounts, $& =~ tr/\n// while /^(.*\n)\1*/gm; print for @newcounts; __DATA__ HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*03:01 HLA-A*03:01 HLA-A*03:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03