rohan.mohapatra47 has asked for the wisdom of the Perl Monks concerning the following question:

My input file is somewhat like this

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

Output will be

CC225|b1|4|Rosha CC225|b2|2|Roshw CC222|b1|2|Roqsha CC222|b2|2|Roqsh

It counts ID on the basis of batches & prints it.

Can someone help?

Replies are listed 'Best First'.
Re: Counting ID on the basis of batch
by Discipulus (Canon) on Sep 23, 2016 at 07:05 UTC
    maybe

    welcome to the monastery rohan.mohapatra47!

    I see this is your first post so please learn how to ask a good question: first of all use <c>code or data here </c> tags.

    Second show some effort ie some line of Perl code you are testing and state clearly where you encounter problem.

    The risk for post like your is to offend some venerable monk ("this is NOT a programming writing service") and get ignored.

    Now go back to your post click Edit and add code tags and some example, put at the end a note saying you edited the node (or my comments will seem out of target!)

    L*

    PS i've forgot to mention How do I post a question effectively?

    PPS ++ to you to have added code tags.

    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.
Re: Counting ID on the basis of batch
by tybalt89 (Monsignor) on Sep 23, 2016 at 14:25 UTC
    #!/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
      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.