Thank you so much, I am almost there. Long time lurker first time poster, apologies about quoting code correctly. Can you explain the command? I don't really need column headers to make things more straight forward.
NR == FNR {T[$1]++; next} # execute only in the first pass reading input
FNR > 1{print $1, T[$1], $2} # skip first line and insert the tally in from previous read after the first column
Some Perl code that could be more flexible.
#!/usr/bin/perl
use strict;
use warnings;
my $filename = shift or die "Usage: $0 FILENAME\n";
my %tally;
open my $fh, '<', $filename or die "Could not open $filename: $!\n";
<$fh>;
my $data_position = tell $fh;
while (my $entry = <$fh>) {
my ($id) = split '\s+', $entry;
$tally{$id}++;
}
seek $fh, $data_position, 0;
while (my $entry = <$fh>) {
my @fields = split '\s+', $entry;
splice @fields, 1,0, $tally{$fields[0]};
print "@fields\n";
}
close $fh;
Save as tally.pl
Run as perl tally.pl pshields1984.input