#!/bin/bash scratch=tmp/ [ ! -d "$scratch" ] && mkdir $scratch rm -r $scratch/* SUFF="_cnt" IFS="," while read pol ch it do [ ! -d "$scratch/`echo $ch | tr ' ' '_'`" ] && mkdir $scratch/`echo $ch | tr ' ' '_'` echo "$it" >> $scratch/`echo $ch | tr ' ' '_'`/`echo $pol | 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