Re: Manipulating file content
On Wed, Jun 28, 2000 at 02:37:33PM -0500, Bolan Meek wrote:
> Viktor Rosenfeld wrote:
> >
> > Hi list,
> >
> > this one is for all the regexp, shell, and editing-experts...
>
> How about us perl hackers, hunh?! Got sumpin' g'inst us, buddy!?
seems to me, us perl folk are the culmination of the cross-breeding
of all those geni. not that that's a good thing... :)
> > Suppose I have a comma-seperated or tab-seperated file and I want
> > to flip the lines and columns. So an input file like:
> >
> > a,1,A
> > b,2,B
> > c,3,C
> >
> > would be transformed into:
> >
> > a,b,c
> > 1,2,3
> > A,B,C
> >
> > Is there an fast and easy way of doing this? Ideally through the shell
> > or with VIM. I would RTFM, but I have no clue which manual to read.
this is the kind of thing perl was designed for.
[OT in a large way]
i'd try something like (tmtowtdi):
#!/usr/bin/perl
$sep = shift || "\t"; # can supply separator on cmd line
@ary = (); # for peace of mind
for ($ln=0; <>; $ln++) {
chomp;
$fn = 0;
foreach (split(/$sep/o)) {
$ary[ $fn++ ][ $ln ] = $_;
#$ary[<col>][<row>] == val
}
}
print join("\n", map {join($sep,@$_)} @ary ),"\n";
alarmingly, it even seems to work well on mismatched tables
such as
a b c
1 2 3 4 5
i ii
A B C D
Reply to: