#!/bin/bash # Text "scaling" # $Id: unix3-scale.sh 191 2006-03-29 11:07:00Z cactus $ # # (C) 2005 Dr. ERDI Gergo # # See http://cactus.rulez.org/elte/2005-1-unix/#3 for a description of what it does # # Licensed under the GNU General Public License, version 2 function help () { self=`basename $0` cat << EOF Usage: $self {-mxn|+mxn} [FILE] Scales or shrinks the contents of FILE (or the standard input) based on MODE. MODE can be +mxn for scaling horizontally by a factor of m, and vertically by a factor of n, or -mxn for shrinking. Options: -help Display this help message (C) 2005 Dr. ERDI Gergo Version: \$Id: unix3-scale.sh 191 2006-03-29 11:07:00Z cactus $ EOF exit 0 } function error () { echo ERROR: $@! >&2 exit 1 } function options () { [ -z "$1" ] && return case "$1" in -help) help ;; -*) mode=shrink mxn=$(echo $1|cut -f2 -d-) m=$(echo $mxn|cut -f1 -dx) n=$(echo $mxn|cut -f2 -dx) shift options "$@" ;; +*) mode=zoom mxn=$(echo $1|cut -f2 -d+) m=$(echo $mxn|cut -f1 -dx) n=$(echo $mxn|cut -f2 -dx) shift options "$@" ;; *) [ $# -gt 1 ] && error "Too many arguments" [ -f "$1" -a -r "$1" ] || error "$1: Unable to open file" exec <"$1" ;; esac } function zoom () { AWKPROG=' BEGIN { FS=""; } { sor = "" for (i = 1; i != NF+1; i++) for (j = 0; j != m; j++) sor = sprintf ("%s%s", sor, $i); for (i = 0; i != n; i++) print sor; }' awk -v m=$m -v n=$n "$AWKPROG" 2>/dev/null } function shrink () { AWKPROG=' BEGIN { FS=""; } { if (sor == 0) { for (i = 1; i < NF+1; i += m) printf "%s", $i; print ""; } sor++; if (sor == n) sor = 0; }' awk -v m=$m -v n=$n "$AWKPROG" 2>/dev/null } options "$@" [ -z $mode ] && error "Scaling mode argument missing" case -$mode in -zoom) zoom ;; -shrink) shrink ;; *) error "Bad scaling mode" ;; esac