in reply to Counting ID on the basis of batch

#!/usr/bin/perl -l # http://perlmonks.org/?node_id=1172420 use strict; use warnings; my %counter; while(<DATA>) { chomp; my @parts = split /\|/; $counter{ join ' ', @parts[0, 4, 1] }++; } for (sort keys %counter) { my @parts = split; print join '|', @parts[0, 1], $counter{$_}, $parts[2]; } __DATA__ CC225|Rosha|xyz|2221|b1 CC225|Rosha|xyz|2230|b1 CC225|Rosha|xyz|2221|b1 CC225|Rosha|xyz|2221|b1 CC225|Roshw|xyz|2222|b2 CC225|Roshw|xyz|2230|b2 CC222|Roqsha|xyz|2222|b1 CC222|Roqsha|xyz|2244|b1 CC222|Roqsh|xyz|2221|b2 CC222|Roqsh|xyz|2111|b2

Replies are listed 'Best First'.
Re^2: Counting ID on the basis of batch -- oneliner
by Discipulus (Canon) on Sep 23, 2016 at 20:07 UTC
    eh no! uneasy question uneasy answer!
    # warning win32 doublequotes perl -F"\W" -lanE "$r{join('|',@F[0,4,1])}++}{say s/(\|\w+$)/\|$r{$_}$ +1/r for keys %r" data.txt

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.