#!/bin/bash # Columniation # $Id: unix4-columniate.sh 191 2006-03-29 11:07:00Z cactus $ # # (C) 2005 Dr. ERDI Gergo # # See http://cactus.rulez.org/elte/2005-1-unix/#4 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 [FILE] Breaks up the contents of FILE (or the standard input) into same-width columns. Options: -help Display this help message (C) 2005 Dr. ERDI Gergo Version: \$Id: unix4-columniate.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 ;; *) [ $# -gt 1 ] && error "Too many arguments" [ -f "$1" -a -r "$1" ] || error "$1: Unable to open file" exec <"$1" ;; esac } options "$@" AWKPROG=' BEGIN { width = 0 } { lines[size++] = $0 if (length() > width) width = length() } END { # Ha 80-nal szelesebb, akkor nem tordelunk if (width > 80) { for (i = 0; i != size; ++i) print lines[i] exit } # Azert nem 80/w, mert kell hely az oszlop-elvalasztoknak is c = int(82 / (width + 2)) l = size / c if (int(l) != l) l = int(l) + 1 for (i = 0; i != l; i++) { sor = lines[i]; for (j = 1; j != c; ++j) { if ((i + j * l) < size) { eddigi_szelesseg = (width + 2) * j format = sprintf("%%-%ds%%s", eddigi_szelesseg) sor = sprintf(format, sor, lines[i + j * l]) } } print sor; } }' awk "$AWKPROG" 2>/dev/null