Friday, July 30, 2010

Quick and Dirty histogram in awk

awk '{histogram[int(($2*100)/100)]++;}END{for (i in histogram) { print i, histogram[i] }}' somerandomfilewithatagandcountdataincol2 | sort -n -k 1 > hist

Print every other line of a file

cat blah | awk 'BEGIN{n=0}{if(n%2==0) print $0;n++;}'

Wednesday, July 28, 2010

Getting started with Cython on Centos 5.4 64Bit

Centos is a nightmare. OK so first up I wanted to do with with Python 2.6 for which there is no official Centos package. So get the geekymedia repos which has a version of Python 2.6 which will install along side the existing 2.4 (and can be access as python26) here: here.

Then download the Cython tarball. Before you build Cython make sure you setup compile flags for 32bitness and make sure python 2.6 gets used i.e.

export CFLAGS="-m32 -march=i386"
export PYTHON="python26"

then run python26 install in the Cython directory. I had to do some other hacking of -m32 and -march=i386 on to things, but I think doing it this way should work. That should build and install Cython.

Put the following example in a file called demo.pyx:

cdef extern from "math.h":
double sin(double)

cdef double f(double x):
return sin(x)

print f(2.0)

Then make the following Makefile. Make sure you have tabs!

demo: demo.pyx
cython demo.pyx --embed
gcc -g -pthread -m32 -march=i386 demo.c -L/usr/lib -lm -lpython2.6 -o ./demo -I/usr/include/python2.6

Then just type make and it should build a binary called demo.