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

Re: [OT] rearranging data files



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thursday 06 June 2002 21:30, Joerg Johannes wrote:
> 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 have written a little python script to do it. Hope it works for you. :-)

#!/usr/bin/env python
import re,sys,string

if len(sys.argv) != 2:
    print "Usage: %s infile" %(sys.argv[0])
    sys.exit()
infile = string.replace(open(sys.argv[1]).read(),"\n"," ")
pattern = re.compile(r"(?:(.+?[ ]) *)",re.DOTALL)
numbers = pattern.findall(infile)
outp = ""
for i in range(len(numbers)):
    if (i%2):
        outp+='\t'
    if float(numbers[i])>=0:
        outp+=' '
    outp+=numbers[i]+(' '*(10-len(numbers[i])))
    if float(numbers[i])>=0: outp+=' '
    if (i%2):
        outp+='\n'
print outp
- --
Matias Hermanrud Fjeld

Computers are like air conditioners.
They stop working when you open Windows.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8/+8oInG+hz/aOQARAsJWAKCp4H5VgVTJbfelNLgBq62zIVu1ZQCfXpAw
GKogJJWopvq5t25LPbjWhYI=
=Uh7v
-----END PGP SIGNATURE-----


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



Reply to: