$ uniq -c sam.txt
1 test1
1 test2
1 test3
3 test1
左側の数値が集計になります。スペースを挟んで行が表示されます。
$ sort sam.txt | uniq -c
4 test1
1 test2
1 test3
出力結果の表示形式を変更したい場合はawk等を使用すると便利です。
$ sort sam.txt | uniq -c | awk 'BEGIN { FS=" " } { print $2 "=" $1}'
test1=4
test2=1
test3=1