head 1.24; access; symbols SHTOOL_2_0_8:1.24 SHTOOL_2_0_7:1.23 SHTOOL_2_0_6:1.22 SHTOOL_2_0_5:1.22 SHTOOL_2_0_4:1.22 SHTOOL_2_0_3:1.21 SHTOOL_2_0_2:1.21 SHTOOL_2_0_1:1.20 SHTOOL_2_0_0:1.20 SHTOOL_2_0b3:1.20 SHTOOL_2_0b2:1.20 SHTOOL_2_0b1:1.20 SHTOOL_1_6:1.16.0.2 SHTOOL_1_6_2:1.16 SHTOOL_1_6_1:1.16 SHTOOL_1_6_0:1.16 SHTOOL_1_5:1.14.0.2 SHTOOL_1_5_4:1.14 SHTOOL_1_5_3:1.14 SHTOOL_1_5_2:1.14 SHTOOL_1_5_1:1.13 SHTOOL_1_5_0:1.13 SHTOOL_1_4_9:1.13 SHTOOL_1_4_8:1.12 SHTOOL_1_4_7:1.11 SHTOOL_1_4_6:1.10 SHTOOL_1_4_5:1.10 SHTOOL_1_4_4:1.10 SHTOOL_1_4_3:1.10 SHTOOL_1_4_2:1.10 SHTOOL_1_4_1:1.10 SHTOOL_1_4_0:1.9 SHTOOL_1_3_3:1.7 SHTOOL_1_3_2:1.5 SHTOOL_1_3_1:1.4 SHTOOL_1_3_0:1.4 SHTOOL_1_2_9:1.1.1.1 SHTOOL_1_2_8:1.1.1.1 SHTOOL_1_2_7:1.1.1.1 SHTOOL_1_2_6:1.1.1.1 SHTOOL_1_2_5:1.1.1.1 SHTOOL_1_2_4:1.1.1.1 SHTOOL_1_2_3:1.1.1.1 SHTOOL_1_2_2:1.1.1.1 SHTOOL_1_2_1:1.1.1.1 SHTOOL_1_2_0:1.1.1.1 SHTOOL_1_1_0:1.1.1.1 RSE:1.1.1; locks; strict; comment @# @; 1.24 date 2008.04.02.20.13.44; author rse; state Exp; branches; next 1.23; commitid eB5Wirx8MdMyDzXs; 1.23 date 2007.05.19.19.09.39; author rse; state Exp; branches; next 1.22; commitid OPUF1UBbllBTwzis; 1.22 date 2006.02.04.09.43.25; author rse; state Exp; branches; next 1.21; commitid oWk7BmnyAitJtfkr; 1.21 date 2005.06.15.08.14.36; author rse; state Exp; branches; next 1.20; 1.20 date 2004.02.12.16.06.27; author rse; state Exp; branches; next 1.19; 1.19 date 2004.01.01.16.54.20; author rse; state Exp; branches; next 1.18; 1.18 date 2003.02.11.13.00.50; author rse; state Exp; branches; next 1.17; 1.17 date 2003.02.11.12.56.40; author rse; state Exp; branches; next 1.16; 1.16 date 2001.12.31.15.06.04; author rse; state Exp; branches; next 1.15; 1.15 date 2001.09.23.10.58.10; author rse; state Exp; branches; next 1.14; 1.14 date 2001.01.17.09.55.11; author rse; state Exp; branches; next 1.13; 1.13 date 2000.03.24.15.22.51; author rse; state Exp; branches; next 1.12; 1.12 date 2000.03.12.11.12.21; author rse; state Exp; branches; next 1.11; 1.11 date 99.12.24.15.11.23; author rse; state Exp; branches; next 1.10; 1.10 date 99.07.06.13.30.48; author rse; state Exp; branches; next 1.9; 1.9 date 99.07.01.15.04.36; author rse; state Exp; branches; next 1.8; 1.8 date 99.07.01.15.02.48; author rse; state Exp; branches; next 1.7; 1.7 date 99.07.01.11.54.08; author rse; state Exp; branches; next 1.6; 1.6 date 99.06.29.17.37.34; author rse; state Exp; branches; next 1.5; 1.5 date 99.06.28.15.18.44; author rse; state Exp; branches; next 1.4; 1.4 date 99.06.17.07.35.08; author rse; state Exp; branches; next 1.3; 1.3 date 99.06.12.14.46.37; author rse; state Exp; branches; next 1.2; 1.2 date 99.06.12.14.08.50; author rse; state Exp; branches; next 1.1; 1.1 date 99.04.29.11.19.50; author rse; state Exp; branches 1.1.1.1; next ; 1.1.1.1 date 99.04.29.11.19.50; author rse; state Exp; branches; next ; desc @@ 1.24 log @adjust copyright for year 2008 @ text @## ## table -- Pretty-print a field-separated list as a table ## Copyright (c) 1998-2008 Ralf S. Engelschall ## ## This file is part of shtool and 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 of the License, or (at your option) any later version. ## ## This file 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; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, ## USA, or contact Ralf S. Engelschall . ## str_tool="table" str_usage="[-F|--field-sep ] [-w|--width ] [-c|--columns ] [-s|--strip ] ..." arg_spec="1+" opt_spec="F:w:c:s:" opt_alias="F:field-sep,w:width,c:columns,s:strip" opt_F=":" opt_w=15 opt_c=3 opt_s=79 . ./sh.common if [ $opt_c -gt 4 ]; then echo "$msgprefix:Error: Invalid number of colums (1..4 allowed only)" 1>&2 shtool_exit 1 fi case "x$opt_F" in x? ) ;; * ) echo "$msgprefix:Error: Invalid separator (one char allowed only)" 1>&2; shtool_exit 1 ;; esac # split the list into a table list=` IFS="$opt_F" for entry in $*; do if [ ".$entry" != . ]; then echo "$entry" fi done |\ awk " BEGIN { list = \"\"; n = 0; } { list = list \\$1; n = n + 1; if (n < $opt_c) { list = list \":\"; } if (n == $opt_c) { list = list \"\\n\"; n = 0; } } END { print list; } " ` # format table cells and make sure table # doesn't exceed maximum width OIFS="$IFS" IFS=' ' for entry in $list; do case $opt_c in 1 ) eval "echo \"\${entry}\" | awk -F: '{ printf(\"%-${opt_w}s\\n\", \$1); }'" ;; 2 ) eval "echo \"\${entry}\" | awk -F: '{ printf(\"%-${opt_w}s %-${opt_w}s\\n\", \$1, \$2); }'" ;; 3 ) eval "echo \"\${entry}\" | awk -F: '{ printf(\"%-${opt_w}s %-${opt_w}s %-${opt_w}s\\n\", \$1, \$2, \$3); }'" ;; 4 ) eval "echo \"\${entry}\" | awk -F: '{ printf(\"%-${opt_w}s %-${opt_w}s %-${opt_w}s %-${opt_w}s\\n\", \$1, \$2, \$3, \$4); }'" ;; esac done |\ awk "{ if (length(\$0) > $opt_s) { printf(\"%s\\n\", substr(\$0, 0, $opt_s-1)); } else { print \$0; } }" IFS="$OIFS" shtool_exit 0 ## ## manual page ## =pod =head1 NAME B - B pretty-print a field-separated list =head1 SYNOPSIS B [B<-F>|B<--field-sep> I] [B<-w>|B<--width> I] [B<-c>|B<--columns> I] [B<-s>|B<--strip> I] IBI... =head1 DESCRIPTION This pretty-prints a list of strings as a table. =head1 OPTIONS The following command line options are available. =over 4 =item B<-F>, B<--field-sep> I Separate columns using I. Default is C<:>. =item B<-w>, B<--width> I Width of each column. Default is 15 characters. =item B<-c>, B<--columns> I Number of columns. Default is C<3>. =item B<-s>, B<--strip> I Strip off any characters past I. Default is C<79>. =back =head1 EXAMPLE # shell script shtool table -F , -w 5 -c 4 "1,2,3,4,5,6,7,8,9,10,11,12" =head1 HISTORY The B B command was originally written by Ralf S. Engelschall Erse@@engelschall.comE in 1999 for B. =head1 SEE ALSO shtool(1), tr(1), fmt(1), sh(1), awk(1), sed(1). =cut @ 1.23 log @Adjust copyright for year 2007. @ text @d3 1 a3 1 ## Copyright (c) 1998-2007 Ralf S. Engelschall @ 1.22 log @Adjust copyright for year 2006 @ text @d3 1 a3 1 ## Copyright (c) 1998-2006 Ralf S. Engelschall @ 1.21 log @Adjust copyright for year 2005 @ text @d3 1 a3 1 ## Copyright (c) 1998-2005 Ralf S. Engelschall @ 1.20 log @Embedded the command documentation from shtool.pod into the individual sh.xxxx scripts and this way split the large shtool(1) manual page into individual manual pages shtool-xxxx(1). Submitted partly by: Thomas Lotterer @ text @d3 1 a3 1 ## Copyright (c) 1998-2004 Ralf S. Engelschall @ 1.19 log @Bump copyright year for 2004 @ text @a3 1 ## Originally written for Apache d88 65 @ 1.18 log @Bump year in copyright messages to cover new year 2003. @ text @d3 1 a3 1 ## Copyright (c) 1998-2003 Ralf S. Engelschall @ 1.17 log @Fixed error exit in all commands by introducing an "shtool_exit" function (which correctly cleans up temporary files) and replacing all "exit" calls with "shtool_exit". THIS THE FIRST TIME INTRODUCED THE USAGE OF A SHELL FUNCTION TO THE SHTOOL CODE. This is known to break on _very_ anchient and broken Bourne-Shell implementations and is considered to be no longer a real problem in the year 2003. Especially there is no alternative way anyway. @ text @d3 1 a3 1 ## Copyright (c) 1998-2002 Ralf S. Engelschall @ 1.16 log @bump copyright years @ text @d36 1 a36 1 exit 1 d40 1 a40 1 * ) echo "$msgprefix:Error: Invalid separator (one char allowed only)" 1>&2; exit 1 ;; @ 1.15 log @*** empty log message *** @ text @d3 1 a3 1 ## Copyright (c) 1998-2001 Ralf S. Engelschall @ 1.14 log @*** empty log message *** @ text @d23 1 a23 1 str_usage="[-F] [-w] [-c] [-s] ..." d26 1 @ 1.13 log @*** empty log message *** @ text @d3 1 a3 1 ## Copyright (c) 1998-2000 Ralf S. Engelschall @ 1.12 log @*** empty log message *** @ text @d2 1 a2 1 ## table -- Pretty print a field-separated list as a table @ 1.11 log @*** empty log message *** @ text @d3 1 a3 1 ## Copyright (c) 1998-1999 Ralf S. Engelschall @ 1.10 log @*** empty log message *** @ text @d25 1 a25 1 opt_spec="F:!w:!c:!s:!" @ 1.9 log @*** empty log message *** @ text @d2 1 a2 1 ## table -- Pretty print a field-sperarated list as a table d39 1 a39 1 * ) echo "$msgprefix:Error: Invalid seperator (one char allowed only)" 1>&2; exit 1 ;; @ 1.8 log @*** empty log message *** @ text @d80 2 a81 2 awk "{ if (length(\$0) > $opt_s) { @ 1.7 log @*** empty log message *** @ text @d52 1 a52 1 { d83 1 a83 1 } else { @ 1.6 log @*** empty log message *** @ text @d69 1 d87 1 @ 1.5 log @*** empty log message *** @ text @d67 1 a67 1 # format table cells and make sure table d83 1 a83 1 print \$0; @ 1.4 log @*** empty log message *** @ text @d31 1 a31 1 . sh.common @ 1.3 log @*** empty log message *** @ text @d23 1 a23 1 str_usage="[-F] [-w] [-c] [-s] ..." @ 1.2 log @*** empty log message *** @ text @d6 15 @ 1.1 log @Initial revision @ text @a0 1 #!/bin/sh d4 1 a4 1 ## Usage: table [-F ] [-w ] [-c ] [-s ] ... d7 16 a22 17 # # command line parsing # width=15 cols=3 sep=':' strip=79 while [ ".$1" != . ]; do case $1 in -F ) sep="$2"; shift; shift; continue ;; -w ) width="$2"; shift; shift; continue ;; -c ) cols="$2"; shift; shift; continue ;; -s ) strip="$2"; shift; shift; continue ;; * ) break ;; esac done case x$sep in d24 1 a24 1 * ) echo "$0:Error: Invalid seperator (one char allowed only)" 1>&2; exit 1 ;; a25 4 if [ $cols -gt 4 ]; then echo "$0:Error: Invalid number of colums (1..4 allowed only)" 1>&2 exit 1 fi a26 1 # a27 1 # d29 1 a29 1 IFS="$sep" d40 1 a40 1 if (n < $cols) { d43 1 a43 1 if (n == $cols) { a51 1 # a53 1 # d57 5 a61 5 case $cols in 1 ) eval "echo \"\${entry}\" | awk -F: '{ printf(\"%-${width}s\\n\", \$1); }'" ;; 2 ) eval "echo \"\${entry}\" | awk -F: '{ printf(\"%-${width}s %-${width}s\\n\", \$1, \$2); }'" ;; 3 ) eval "echo \"\${entry}\" | awk -F: '{ printf(\"%-${width}s %-${width}s %-${width}s\\n\", \$1, \$2, \$3); }'" ;; 4 ) eval "echo \"\${entry}\" | awk -F: '{ printf(\"%-${width}s %-${width}s %-${width}s %-${width}s\\n\", \$1, \$2, \$3, \$4); }'" ;; d65 2 a66 2 if (length(\$0) > $strip) { printf(\"%s\\n\", substr(\$0, 0, $strip-1)); @ 1.1.1.1 log @Import of shtool 1.1.0 @ text @@