[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] rearranging data files



Well I felt like procrastinating today, so here's a solution:

#!/usr/local/bin/perl
# pivot.pl - turn rows into columns
while (<>) {
    chomp;
    s/^\s+//;
    push(@rows, [split(/\s+/,$_)]);
}
shift(@$_) for @rows;
print join("\t", map(shift(@$_), @rows)), "\n" while (@{$rows[0]});



Note that this assumes each row has the same number of observations as the
others, since it just tests whether it's printed all the observations in
the first row.  If you need it to be possible for rows to be different
lenths, you'll need a more complicated test function.

----------------------------------------------------------------------
Andrew J Perrin - http://www.unc.edu/~aperrin
Assistant Professor of Sociology, U of North Carolina, Chapel Hill
clists@perrin.socsci.unc.edu * andrew_perrin (at) unc.edu


On Thu, 6 Jun 2002, Joerg Johannes wrote:

> Hi list
> 
> At university, we have an old spectro-photometer, that spits out its data in ASCII formatted
> files. The problem is, The data I get is sorted in lines rather than in columns (See examples
> below). Is there a progam (preferably command-line) that can do swap the rows to columns,
> and vice-versa. I need something that makes "Search and replace two spaces by a \newline"
> and afterwards a bit of rectangle-editing in emacs obsolete.
> 
> Here comes the example:
> ... The file I get from the photometer ...
>    0.000  -0.000014   0.007990   0.013480   0.017795   0.021675   0.028663   0.032588   0.038895   0.044002   0.050834 
>    1.000  -0.000962   0.038026   0.069427   0.085865   0.108307   0.140563   0.166330   0.195794   0.223759   0.252516 
> --- end file ---
> 
> ... And I have to change it to ...
> -0.000014    -0.000962
>  0.007990     0.038026
>  0.013480     0.069427
>  0.017795     0.085865
>  0.021675     0.108307
>  0.028663     0.140563
>  0.032588     0.166330
>  0.038895     0.195794
>  0.044002     0.223759
>  0.050834     0.252516
> --- end file ---
> 
> This is a rather short example, this is OK to be done by hand, but imagine 20 rows of 20
> samples... Annoying, isn't it?
> 
> Thanks for any pointer
> 
> joerg
> 
> 
> -- 
> To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
> 
> 


-- 
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org



Reply to: