Subject: Re: text processing tool
To: Charles Blundell <cb@kittenz.org>
From: James Wetterau <jwjr@panix.com>
List: netbsd-help
Date: 03/12/2003 12:42:38
Charles Blundell says:
> on Wed, Mar 12, 2003 at 04:13:29PM +0100, Lubos Vrbka wrote:
> > does in netbsd exist any utility that would convert column-formatted
> > text file to row-formatted text file? (eqivalent to matrix
> > transposition, i.e.:
>
> rs(1) can do this:
... And here I just bothered to write this Perl script. Oh well, at
least I can be happy in the knowledge that mine formats the output
in columns in case the input is not of uniform column width:
#!/usr/pkg/bin/perl -w
use strict;
my @l = <>;
my (@w, $o);
foreach(@l) { push @w, maxp1(map { length } split ) }
while(1){
$o = join "", map { $l[$_] =~ s/(\S*)\s*//; sprintf "%$w[$_]s", "$1"; }
(0..$#l);
last if $o=~m/^\s*$/;
print "$o\n";
}
sub maxp1
{
my $max = 0;
foreach(@_) { $max = $_ if ($_>$max) }
return ++$max;
}
--
James Wetterau, Jr. jwjr@panix.com (h) james@surgam.net (w)
"The majority is never right. Never, I tell you! That's one of these
lies in society that no free and intelligent man can help rebelling
against." -- Dr. Stockman in "An Enemy of the People", by Henrik Ibsen