perlnewbie012215 has asked for the wisdom of the Perl Monks concerning the following question:

awk 'NR==FNR{a[$1]++;next;}{ if (a[$1] > 1)print;}' imageoutcome.txt imageoutcome.txt

this code prints duplicate rows basing on the first column, can you guys help me write in perl

Replies are listed 'Best First'.
Re: how to change this code into perl
by kcott (Archbishop) on Aug 30, 2015 at 07:04 UTC

      Wow, nice! I thought this wouldn't work, but it turns out that a2p seems to handle it correctly, even with the FNR variable, and even gives not too ugly code,

      I guess I just had disappointments with s2p, which doesn't even know about the gsed extension of semicolons working as statement separators.

Re: how to change this code into perl
by BrowserUk (Patriarch) on Aug 30, 2015 at 06:56 UTC
      I am afraid this:
      perl -anle"next unless @L; print if $L[0] eq $F[0]; @L = @F;" in.txt > + out.txt
      will never enter into the loop because the next statement at the beginning will prevent @L from ever being set.

      Perhaps this instead:

      perl -anwle 'BEGIN{$L = "";} print if $F[0] eq $L; $L = $F[0];' in.tx +t > out.txt
      Although the BEGIN block isn't really necessary if the warnings are not activated:
      perl -anle 'print if $F[0] eq $L; $L = $F[0];' in.txt > out.txt

        Thank you Laurent_R!!! the one liner is not printing all the lines, say I have three duplicates its only printing the last two or one duplicate, not all of them.

        1 twenty 2 thirty 1 forty 1 fifty
        output 1 twenty 1 forty 1 fifty

        is there a way to script it instead of a oneliner. Thank you guys