Frequency Counts Using a Shell Script
This shell script can generate frequencies and it includes options for choosing the records per case and subsetting based on that (for a hierarchical data file). It depends on another shell script (printrec0), linked below.#! /bin/sh #. Generate frequencies given a file, columns, and optional rec # and rec/case case $# in 0) echo " "; echo " usage: freq filename column(s) [r=record rc=records per case]" ; echo " example: freq test.dat 8-13 r=2 rc=5" ; echo " "; exit 0;; # *) echo " "; # echo " error: ** syntax error/incorrect number of arguments **" ; # echo " usage: freq filename column(s) [r=record rc=records per case]" ; # echo " example: freq test.dat 8-13 r=2 rc=5" ; # echo " "; # exit 1;; 2) echo " "; echo "freq of $1, column(s) $2"; echo " "; echo " freq value"; echo " "; cut -c$2 $1 | sort | uniq -c > /tmp/frtmp$$; cat /tmp/frtmp$$; awk 'BEGIN {print "-------------"} {n += $1} END { printf(" %d cases\n",n)}' /tmp/frtmp$$; rm -f /tmp/frtmp$$;; 4) echo " "; echo "freq of $1, column(s) $2, record $3, $4 records per case"; echo " "; echo " freq value"; echo " "; awk -f printrec0 $3 $4 $1 > /tmp/frtmp2$$; cut -c$2 /tmp/frtmp2$$ | sort | uniq -c > /tmp/frtmp$$; cat /tmp/frtmp$$; awk 'BEGIN {print "-------------"} {n += $1} END { printf(" %d cases\n",n)}' /tmp/frtmp$$; rm -f /tmp/frtmp$$; rm -f /tmp/frtmp2$$;; esac echo " " exit 0(Note: credit for this shell script goes to Nelson Martinez).
Back to Frequency Count Page
Last modified: Sat Sep 6 17:21:22 EDT 2025