perl -wne ' $h{$1}++ if /bn(\d{4}),/ }{$,="\t"; print %h, "\n" ' datafile