I have a tab delimited file that looks like this:
Het 157709 157731 Cluster.90 2 +
Het 157739 157760 Cluster.90 2 +
Het 164238 164259 Cluster.97 10 +
Het 164380 164401 Cluster.97 10 +
Het 164396 164417 Cluster.97 10 +
Het 164397 164421 Cluster.97 10 +
Het 164397 164420 Cluster.97 10 +
Het 164399 164420 Cluster.97 10 +
Het 164536 164561 Cluster.97 10 +
Het 164576 164598 Cluster.97 10 +
Het 164599 164615 Cluster.97 10 +
Het 164635 164656 Cluster.97 10 +
Het 198007 198031 Cluster.125 3 +
Het 198007 198028 Cluster.125 3 +
Het 198011 198035 Cluster.125 3 +
I am looking for an efficient way to create a file as follows:
Het 157709 157760 Cluster.90 2 +
Het 164238 164656 Cluster.97 10 +
Het 198007 198035 Cluster.125 3 +
Where for each unique entry in column 4 I write a line that includes the first row for columns 1 and 2, followed by the last row in columns 3, 4, 5 and 6. So far I have tried the following solution but it seems very inefficient:
for i in `awk '{print $4}' filename | sort | uniq`
do
fgrep -F $i -w filename | awk 'NR==1 {printf $1"\t"$2"\t"} END {print $3"\t"$4"\t"$5"\t"$6}' >>filename2
done
The problem is that when I have a huge file (487559 lines), it takes forever. Is there a better solution lurking in someone's head there?
source
share