#! /bin/sh # Copyright (C) 1998 Stephane Levant # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. version=1.0 prog=`basename $0` version () { cat << EOF $prog $VERSION EOF exit } usage() { cat << EOF >&2 Usage: $prog [OPTIONS] [FILE]... Display percentage usage of each character in FILE if FILE is omited, use STDIN. Options: -r REGEXP : use only characters witch match regexp -s : sort -a : use only alphabetics characters (equivalent to -r '[a-z]') -c : respect the case. the default is to assume upper-case and lower-case characters are the same EOF exit 0 } # Display an error message error() { echo "$prog: $1" >&2 exit 1 } [ "$1" == "--help" ] && usage [ "$1" == "--version" ] && version sort=+1 case=1 reg=. while getopts ":hvsacr:" i; do case "$i" in h*) usage ;; v*) version ;; a) reg=[a-z] ;; s) sort=-n ;; r) reg= "$OPTARG" ;; c) case=0 ;; *) error "Invalid option: $OPTARG. Try $prog -h" ;; esac done continue=yes while [ $continue = yes -a $# -gt 0 ] ; do case $1 in --) shift; continue=no ;; -*) shift;; *) continue=no ;; esac done gawk -v case=$case -v reg="$reg" ' { if (case) $0=tolower($0); for(i=1;i<=length($0);i++) { char=substr($0,i,1) if (char ~ reg) { t[char]++ tot++ } } } END { for (i in t) printf("%2.2f %s\n",t[i]/tot*100,i); } ' "$@" | sort $sort