Wednesday, 28 August 2013

split sorted file using python at change of value

split sorted file using python at change of value

I am new to python. My requirement, which is simple if I have to do it
using awk, is as below,
File (test.txt) mentioned below is tab separated,
1 a b c
1 a d e
1 b d e
2 a b c
2 a d e
3 x y z
The output I want it like
file 1.txt should have below values a b c
a d e
b d e
file 2.txt should have below values
a b c
a d e
file 3.txt should have below values
x y z
Original file is sorted on first column. I do not know the row number at
which I have to split. It has to be on change of value. Using awk, I would
write it like
awk -F"\t" 'BEGIN {OFS="\t";} {print $2","$3","$4 > $1}' test.txt
(performance wise, will python be better?)

No comments:

Post a Comment