Recently I had a problem at hand - I had to parse through a huge text log to parse through mpeg section filter data, to search for lost sections. Since this problem did not occur every test cycle, I had to generate the log again and again and parse through it each time. Obviously, the easiest way was to write a program to do this parsing job.
I know good C/C++, and little bit of python. Although I am not an expert at python, I thought using a scripting language would be much easier here, and I was not wrong. Here’s the code:
#Look for what we want
f_input = open(”log1.txt”)
f_output = open(”result.txt”, “w”)
slist = f_input.readlines();
i = 0
prev = 0
prev_orig = “00″
while i < (len(slist) - 1) :
if slist[i].find("Message_Marker") != -1:
s1 = slist[i+1]
s2 = s1[22:24]
d1 = string.atoi(s2,16)
d2 = (d1 >> 1) & 0×1f # version is bits 1-6
if d2 != (prev+1):
if ((d2 == 0) and (prev == 0×1f)): #This is normal rollover
d2 = 0 #dummy
f_output.write(prev_orig + ” ” + s2 + ” ” + hex(prev) + ” ” + hex(d2) + “\n”)
prev = d2
prev_orig = s2
i = i + 1
I am absolutely certain that the above code can be written in much better ways, but the above works well for the purpose. But the point is, it took barely about 15 minutes to develop the above script, and I was happily parsing logs and finding the lost sections.
I look forward to trying out some ruby scripting. Of course, I do not want to compare python/ruby here (that is being done in lot of other forums), I rather choose to play with both and take my time to settle into one of those, or use both in places appropriate.