perl -lane '$h{$_}++ for @F; END{print qq{$h{$_} $_\n} for sort keys %h}' file