views:

482

answers:

3

Hello everybody ,

I have a .txt file like:

Symbols from __ctype_tab.o:

Name                  Value   Class        Type         Size     Line  Section

__ctype             |00000000|   D  |       OBJECT   |00000004|     |.data
__ctype_tab         |00000000|   r  |       OBJECT   |00000101|     |.rodata


Symbols from _ashldi3.o:

Name                  Value   Class        Type         Size     Line  Section

__ashldi3           |00000000|   T  |       FUNC      |00000050|     |.text

How can i parsr this file and get the functions with type FUNC ? Also,from this txt how can i parse and extract .o name ?

How can i get them by column wise parsing or else how.

I need an immediate help...Waiting for an appropriate solution as usual

+2  A: 

Here is a basic approach. What do you think?

# Suppose you have filename "thefile.txt"
import re

obj = ''
for line in file('thefile.txt'):
    # Checking for the .o file
    match = re.search('Symbols from (.*):', line)
    if match:
        obj = match.groups()[0]

    # Checking for the symbols.
    if re.search('|', line):
        columns = [x.strip() for x in a.split('|')]
        if columns[3] == 'FUNC':
            print 'File %s has a FUNC named %s' % (obj, columns[0])
jhs
You're going to get an IndexError (index out of range) with that, if given that text verbatim.
htw
Whoops, good point. I think the newer version won't have that since it only splits if there is a | character in the line.
jhs
+1 for extracting the .o name
Jarret Hardie
+8  A: 
for line in open('thefile.txt'):
  fields = line.split('|')
  if len(fields) < 4: continue
  if fields[3].trim() != 'FUNC': continue
  dowhateveryouwishwith(line, fields)
Alex Martelli
Short and sweet
Jarret Hardie
Eww, tab length 2.
superjoe30
Seriously? In a markup-language environment like SO, where markdown or HTML is the norm, you're critiquing the exact number of indent spaces?
Jarret Hardie
It would be nice if this solution could be adapted to solve the other part of the OP's question: how to track the .o file. I like this answer's brevity compared to my own but it doesn't solve the total problem.
jhs
Do you have any idea who ( http://en.wikipedia.org/wiki/Alex_Martelli ) you are saying eww to, Joe?
bvmou
+3  A: 

I think this might cost less than the use of regexes though i am not totally clear on what you are trying to accomplish

symbolList=[]
for line in open('datafile.txt','r'):
if '.o' in line:
 tempname=line.split()[-1][0:-2]
            pass

if 'FUNC' not in line:
 pass

else:
 symbolList.append((tempname,line.split('|')[0]))

I have learned from other posts it is cheaper and better to wrap up all of the data when you are reading through a file the first time. Thus if you wanted to wrap up the whole datafile in one pass then you could do the following instead

fullDict={}
for line in open('datafile.txt','r'):
    if '.o' in line:
        tempname=line.split()[-1][0:-2]
    if '|' not in line:
        pass
    else:
        tempDict={}
            dataList=[dataItem.strip() for dataItem in line.strip().split('|')]
            name=dataList[0].strip()
            tempDict['Value']=dataList[1]
            tempDict['Class']=dataList[2]
            tempDict['Type']=dataList[3]
            tempDict['Size']=dataList[4]
            tempDict['Line']=dataList[5]
            tempDict['Section']=dataList[6]
            tempDict['o.name']=tempname
            fullDict[name]=tempDict
            tempDict={}

Then if you want the Func type you would use the following:

funcDict={}
for record in fullDict:
    if fullDict[record]['Type']=='FUNC':
        funcDict[record]=fullDict[record]

Sorry for being so obsessive but I am trying to get a better handle on creating list comprehensions and I decided that this was worthy of a shot

PyNEwbie
Well, you're my competitor, but I've got to +1 since I didn't know the 'substr' not in 'larger string' idiom. Thanks!
jhs
but since dictionary is mutable its ceating a great problem.Since the field 'Name' will be repeating in other .o's they r getting overwritten. Any solution for this ?
Sorry that wasn't clear. I lot of times I create tuples as dictionary keys. Without fully knowing your data I would set the key tofullDict[(name, tempname)]=tempdict
PyNEwbie