#!/usr/bin/perl use strict; use warnings; my $prev_col; my $counter; while ( ) { chomp; my ($col, $rest) = $_ =~ /^([^\s]+)(\s.*)$/; if ( ! defined $prev_col || $col ne $prev_col ) { $counter++; $prev_col = $col; } print "$col $counter$rest\n"; } __DATA__ aa one two three aa four five six xy z xy a xy b cc asdfasdf