#!/bin/bash # Text rotation # $Id: unix2-rotate.sh 191 2006-03-29 11:07:00Z cactus $ # # (C) 2005 Dr. ERDI Gergo # # See http://cactus.rulez.org/elte/2005-1-unix/#2 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 [-c] [FILE] Rotates the contents of FILE (or the standard input) by 90 degrees. Options: -c Replace certain graphical characters with their rotated counterparts -help Display this help message (C) 2005 Dr. ERDI Gergo Version: \$Id: unix2-rotate.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 ;; -c) do_rotate_chars=1 shift options "$@" ;; *) [ $# -gt 1 ] && error "Too many arguments" [ -f "$1" -a -r "$1" ] || error "$1: Unable to open file" exec <"$1" ;; esac } # Mivel a feladat lenyegebol kovetkezik, hogy ismernunk kell a leghosszabb # sort, nem tudjuk "azonnal" feldolgozni, hanem ideiglenesen el kell eloszor # tarolnunk az osszes sort. # Ez biztositja, hogy programunk lassu es nagy memoriaigenyu is lesz. # # Eredetileg pure Bash-ben irtam meg, de az borzaszto lassu lett (2 # perces teszt-futasidok, ugyhogy Awk lett belole. function rotate () { AWKPROG=' BEGIN { width = 0; } { lines[size++] = $0; if (length () > width) width = length (); } END { for (i = width; i != 0; i--) { empty_line = 1; lastline = ""; for (j = 0; j < size; j++) { if (length (lines[j]) < i) c = " "; else c = substr (lines[j], i, 1); lastline = lastline c; if (c != " ") empty_line = 0; } if (empty_line) { empty_lines[e++] = lastline } else { if (seen_nonempty_line) for (j = 0; j < e; j++) print empty_lines[j]; delete empty_lines; e = 0; seen_nonempty_line = 1; print lastline; } } }' awk "$AWKPROG" 2>/dev/null } function rotate_chars () { [ -z $do_rotate_chars ] && (cat; return) # Hogy en mit szenvedtem, mire megfelelo " es ' vedelmeket # talaltam ezekhez... local class1='\\/`,'"'"'_\-|' local class2='/\\,'"'"',||_' tr "$class1" "$class2" 2>/dev/null } options "$@" rotate | rotate_chars