tags:

views:

51

answers:

3

I am doing visualization analysis on a trace file I generate from ns-2 that traces out the packets sent/received/dropped at various times of the simulation

here is a sample trace output - http://pastebin.com/aPm3EFax

I want to filter out the column1 after grouping it into S/D/R separately, so that I can sum it over to separately to find packet delivery fraction.

I am clueless on how to get this done? (maybe some awk/python help?)

UPDATE: okay, I did this -

cut -d' ' -f1 wireless-out.tr | grep <x> | wc -l

where <x> is either s or r or D

A: 
import collections
result=collections.defaultdict(list)
with open('data','r') as f:
    for line in f:
        line=line.split()
        key=line[0]
        value=float(line[1])
        result[key].append(value)
for key,values in result.iteritems():
    print(key,sum(values))

yields:

('s', 160.15817391900003)
('r', 80.058963809000005)
('D', 80.195127232999994)

Is this close to the form you want?

unutbu
heh! your solution helped me build up my program! once I could read and split the columns, I started using tuples/lists/dicts to calculate exactly what I was looking for, right now I am looking at my shiny graph drawn by MATLAB, thank you :)
Vaibhav Bajpai
@Vaibhav Bajpai: Yay! glad you found it useful.
unutbu
A: 
import csv
import itertools

data =  csv.reader(open('aPm3EFax.txt', 'rb'), delimiter=' ')

result = [(i, sum(float(k[1]) for k in g)) 
 for i, g in itertools.groupby(sorted(list(data)), key=lambda x: x[0])]
singularity
+1  A: 

Give this a try:

awk '{data[$1]+=$2} END{for (d in data) print d,data[d]}' inputfile

Output:

D 80.1951
r 80.059
s 160.158
Dennis Williamson
It's funny comparing this Awk one-liner to the Python solutions.
JUST MY correct OPINION