in reply to (OT)Count the Column values if it same in the column

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