Du bist hier: Snippet-Verzeichnis » Perl (198)
Sprache:

align

Sprache: English
Programmiersprache: Perl
Veröffentlicht von: tbarron [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1058


Beschreibung

align ragged columns for easy reading and sorting

Code

1 #!/usr/bin/perl 2 $version = "1.2 / updated: <2000-11-05>"; 3 # ======================================================================== 4 # @(#) align - align ragged columns for easy reading and sorting 5 # Copyright (c) 1997 Tom Barron <tbarron@mindspring.com> 6 # released under the GNU Public License -- details below or 'perldoc align' 7 # ======================================================================== 8 # History: 9 # 10 # 1997-04-05 tb created 11 # 2000-11-05 tb fixed long-standing problem with seeking on STDIN 12 # perldoc added 13 # added -v option to display version information 14 # ======================================================================== 15 require "getopts.pl"; 16 17 Getopts( "c:f:o:d:v" ); 18 19 if ($opt_v) 20 { 21 print "align $version\n"; 22 exit; 23 } 24 25 if ($opt_f ne "") 26 { 27 close( STDIN ); 28 open( STDIN, "< $opt_f" ); 29 } 30 31 if ($opt_o ne "") 32 { 33 close( STDOUT ); 34 open( STDOUT, "> $opt_o" ); 35 } 36 37 $ics = (0 == $opt_c) ? 2 : $opt_c; 38 $delimiter = ("" eq $opt_d) ? " " : $opt_d; 39 40 @lines = <STDIN>; 41 for $line (@lines) 42 { 43 chomp( $line ); 44 @words = split( $delimiter, $line ); 45 for ($idx = 0 ; $idx <= $#words ; $idx++) 46 { 47 if ($width[$idx] < length($words[$idx])) 48 { 49 $width[$idx] = length( $words[$idx] ); 50 } 51 } 52 } 53 54 for $line (@lines) 55 { 56 chomp( $line ); 57 @words = split( $delimiter, $line ); 58 for ($idx = 0 ; $idx <= $#words ; $idx++) 59 { 60 if (substr( $words[$idx], 0, 1 ) eq "0") # a number 61 { 62 for ($kdx = length($words[$idx]) ; $kdx < $width[$idx] ; $kdx++) 63 { 64 print( " " ); 65 } 66 print( "$words[$idx]" ); 67 } 68 elsif ($words[$idx] eq "") # empty 69 { 70 print( "-" ); 71 for ($kdx = 1 ; $kdx < $width[$idx] ; $kdx++) 72 { 73 print( " " ); 74 } 75 } 76 elsif ($words[$idx] + 0 == 0) # non-numeric string 77 { 78 print( "$words[$idx]" ); 79 for ($kdx = length($words[$idx]) ; $kdx < $width[$idx] ; $kdx++) 80 { 81 print( " " ); 82 } 83 } 84 else # a number 85 { 86 for ($kdx = length($words[$idx]) ; $kdx < $width[$idx] ; $kdx++) 87 { 88 print( " " ); 89 } 90 print( "$words[$idx]" ); 91 } 92 93 for ($kdx = 0 ; $kdx < $ics ; $kdx++) 94 { 95 print( " " ); 96 } 97 } 98 print( "\n" ); 99 } 100 101 # ======================================================================== 102 103 =head1 NAME 104 105 align - line up ragged columns for easy reading and sorting 106 107 =head1 SYNOPSIS 108 109 align [-c N] [-d STRING] [-f INPUT] [-o OUTPUT] 110 111 align -v 112 113 =list 114 115 =item -c N 116 117 N is the number of spaces inserted between columns in the output 118 119 =item -d STRING 120 121 STRING is used as a delimiter to split each line into columns. 122 Enclose it in quotes if it contains whitespace characters. 123 124 =item -f INPUT 125 126 read INPUT rather than STDIN 127 128 =item -o OUTPUT 129 130 write output to OUTPUT rather than STDOUT 131 132 =item -v 133 134 display the program's version information and exit 135 136 =head1 DESCRIPTION 137 138 align reformats a series of lines into columns to make the information 139 easier to read or sort. It can be helpful, for example, with df on 140 systems with long names on the file systems that cause df's output to 141 be misaligned. 142 143 =head1 ENVIRONMENT 144 145 align works anywhere perl is installed. 146 147 To make align easily runnable under Windows, paste the following into 148 align.bat: 149 150 @echo off 151 perl -S align %1 %2 %3 %4 %5 %6 %7 %8 %9 152 153 Note that this assumes that perl is installed and align and align.bat 154 are in a PATHable directory. 155 156 =head1 LICENSE / COPYING 157 158 This program is free software; you can redistribute it and/or 159 modify it under the terms of the GNU General Public License 160 as published by the Free Software Foundation; either version 2 161 of the License, or (at your option) any later version. 162 163 This program is distributed in the hope that it will be useful, 164 but WITHOUT ANY WARRANTY; without even the implied warranty of 165 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 166 GNU General Public License for more details. 167 168 You should have received a copy of the GNU General Public License 169 along with this program; if not, you can get a copy at 170 http://computer-guy.hypermart.net/software/gpl.html or by writing 171 to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 172 MA 02139, USA. 173 174 =head1 AUTHOR 175 176 Tom Barron <tbarron@mindspring.com> 177 178 =cut 179

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS