#!/bin/bash scratch=tmp/ [ ! -d "$scratch" ] && mkdir $scratch rm -r $scratch/* SUFF="_cnt" IFS="," while read pol ch it do [ ! -d "$scratch/$pol" ] && mkdir $scratch/$pol echo "$it" >> $scratch/$pol/`echo $ch | tr ' ' _` done < $1 for dir in $scratch* do for file in $dir/* do echo $(cat $file | sort -n | uniq -c | sed 's/^[ \t]*//') >> $file$SUFF done done