#!/bin/bash # Table to list converter # $Id: unix6-table2list.sh 191 2006-03-29 11:07:00Z cactus $ # # (C) 2005 Dr. ERDI Gergo # # See http://cactus.rulez.org/elte/2005-1-unix/#6 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] Converts the table in FILE (or the standard input) into a list, each one labelled with the name of the column Options: -help Display this help message (C) 2005 Dr. ERDI Gergo Version: \$Id: unix6-table2list.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 } # Igazabol a lenyegi reszt AWK-ban irtam meg. # Magyarazatot nem igenyel, maximum az hogy az utolso blokk utan nem # kell ures sor, ezert pufferelunk egy blokknyit. AWKPROG=' BEGIN { FIRSTLINE = 1 FS = " " } { if (FIRSTLINE) { for (i=1; i <= NF; i++) HEADERS[i] = $i FIRSTLINE=0 } else { if (LASTBLOCK) print LASTBLOCK LASTBLOCK = "" for (i = 1; i <= NF; i++) LASTBLOCK = sprintf ("%s%s: %s\n", LASTBLOCK, HEADERS[i], $i) } } END { if (LASTBLOCK) printf "%s", LASTBLOCK } ' options "$@" awk "$AWKPROG" 2>/dev/null