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

Re: [OT] rearranging data files



On Thu, Jun 06, 2002 at 09:30:30PM +0200, 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?

I'm an awk fan. So my col2rows script looks like this:
    #!/bin/sh

    set -e

    td=${TMPDIR:-/tmp}/col2rows.$$
    mkdir $td
    cd $td

    awk 'BEGIN { columns=0; }
    {
        if (NF > columns)
            columns = NF;

        for (i=1; i<= NF; i++)
            printf("%s\t", $i) >> i;
    }
    END {
        for (i=1; i <= columns; i++)
        {
            system("cat " i);
            printf("\n");
        }
    }
    '

    cd /
    rm -rf $td

The output won't be pretty unless all of the input rows (read from
stdin) have the same number of columns. And the output lines have a
trailing TAB character. Probably could have been done a lot neater with
perl though...

HTH

-- 
Karl E. Jørgensen
karl@jorgensen.com
www.karl.jorgensen.com
... An rfc2324 advocate
http://www.rfc.net/rfc2324.html

Attachment: pgpe_aclZgbr4.pgp
Description: PGP signature


Reply to: