Here is a solution in Perl.

Working, tested code:

use strict; use warnings; my @field_names = qw( PdbId MetalAtm MetalName MetalNumber MetalChn AtmName ResName ResNumber ResChn Distance ); # Trick for concise sample data. sub aref_to_href { my @field_values = @_; die unless @field_names == @field_values; my %h; @h{@field_names} = @field_values; \%h; } my @records_AoH = map { aref_to_href( @{$_} ) } ( [ qw( 1nc7 MG MG 1501 - CL CL 1401 - 3.065 ) ], [ qw( 1nc7 MG MG 1501 - CL CL 1402 - 2.660 ) ], [ qw( 1pex CA CA 503 - CL CL 501 - 2.895 ) ], [ qw( 1qhu NA NA 3 - CL CL 1 - 3.096 ) ], [ qw( 1qjs NA NA 513 A CL CL 511 A 3.096 ) ], [ qw( 1rtg CA CA 2 - CL CL 1 - 3.079 ) ], [ qw( 1xj6 NA NA 5002 - CL CL 5001 - 2.628 ) ], [ qw( 2caj NI NI 1151 B CL CL 1150 B 3.551 ) ], [ qw( 2oa9 CD CD 911 - CL CL 800 - 2.291 ) ], [ qw( 2oa9 CD CD 913 - CL CL 801 - 2.403 ) ], # Added for testing different ResName columns [ qw( 99zz CD CD 911 - CL YY 800 - 2.291 ) ], [ qw( 99zz CD CD 913 - CL YY 801 - 2.403 ) ], ); my %count; $count{ $_->{PdbId} }{ $_->{ResName} }++ foreach @records_AoH; # Uncomment the second `grep` line if needed. # (See the SQL discussion in the previous node). my @filtered_AoH = grep { $count{$_->{PdbId}}{$_->{ResName}} >= 2 } # grep { $_->{ResName} eq 'CL' } @records_AoH; my $template = '%5s %8s %9s %11s %8s %7s %7s %9s %6s %8s'; printf "$template\n", @field_names; foreach (@filtered_AoH) { printf "$template\n", @{ $_ }{@field_names}; }
Output:
PdbId MetalAtm MetalName MetalNumber MetalChn AtmName ResName ResNumbe +r ResChn Distance 1nc7 MG MG 1501 - CL CL 140 +1 - 3.065 1nc7 MG MG 1501 - CL CL 140 +2 - 2.660 2oa9 CD CD 911 - CL CL 80 +0 - 2.291 2oa9 CD CD 913 - CL CL 80 +1 - 2.403 99zz CD CD 911 - CL YY 80 +0 - 2.291 99zz CD CD 913 - CL YY 80 +1 - 2.403
Output with the second `grep` uncommented:
PdbId MetalAtm MetalName MetalNumber MetalChn AtmName ResName ResNumbe +r ResChn Distance 1nc7 MG MG 1501 - CL CL 140 +1 - 3.065 1nc7 MG MG 1501 - CL CL 140 +2 - 2.660 2oa9 CD CD 911 - CL CL 80 +0 - 2.291 2oa9 CD CD 913 - CL CL 80 +1 - 2.403


In reply to Re: (OT)Count the Column values if it same in the column by Util
in thread (OT)Count the Column values if it same in the column by editi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.