#!/usr/bin/perl # http://perlmonks.org/?node_id=1146923 use strict; use warnings; $_ = join "\t", qw(1 A 1 B 2 G 2 H 2 V); 1 while s/\b((\d+)\s\S+)(.*?)\s\2\s(\S+)/$1:$4$3/; print "$_\n";