#!/bin/bash scratch=tmp [ ! -d "$scratch" ] && mkdir $scratch rm -r $scratch/* SUFF="_cnt" IFS="," while read pol ch it rang nw pr inc do [ ! -d "$scratch/$pol" ] && mkdir $scratch/$pol godir=$scratch/$pol echo "$rang" >> $godir/`echo $ch | sed 's/^[ ]*//'` 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