Sunday, April 3, 2011

Parsing each field and process it using 'awk'/'gawk'

Hi all,

Here is a query:

grep bar 'foo.txt' | awk '{print $3}'

The field name emitted by the 'awk' query are mangled C++ symbol names. I want to pass each to dem and finally output the output of 'dem'- i.e the demangled symbols.

Assume that the field separator is a ' ' (space).

From stackoverflow
  • How about

    grep bar 'foo.txt' | awk '{ print $3 }' | xargs dem | awk '{ print $3 }'
    
    Charles Duffy : The way I read the dem man page, the symbol names should be on the command line rather than stdin
    David Zaslavsky : oops, I misread that. I edited in a call to xargs which should fix it.
    Johannes Schaub - litb : well, the c++ side could have spaces in them :)
    David Zaslavsky : darn, I was hoping that wouldn't be the case...
    Johannes Schaub - litb : yeah my c++filt stuff won't work with his (apparently) Sun mangling style :/. but you may lucky maybe dem doesn't put spaces into the c++ side? from what the examples show, it omits showing the return type, at least
  • awk is a pattern matching language. The grep is totally unnecessary.

    awk '/bar/{print $3}' foot.txt
    

    does what your example does.

    Edit Fixed up a bit after reading the comments on the precedeing answer (I don't know a thing about dem...):

    You can make use of the system call in awk with something like:

    awk '/bar/{cline="dem " $3; system(cline)}' foot.txt
    

    but this would spawn an instance of dem for each symbol processed. Very inefficient.

    So lets get more clever:

    awk '/bar/{list = list " " $3;}END{cline="dem " list; system(cline)}' foot.txt
    

    BTW-- Untested as I don't have dem or your input.


    Another thought: if you're going to use the xargs formulation offered by other posters, cut might well be more efficient than awk. At that point, however, you would need grep again.

  • This will print the demangled symbols, complete with argument lists in the case of methods:

    awk '/bar/ { print $3 }' foo.txt | xargs dem | sed -e 's:.* == ::'
    

    This will print the demangled symbols, without argument lists in the case of methods:

    awk '/bar/ { print $3 }' foo.txt | xargs dem | sed -e 's:.* == \([^(]*\).*:\1:'
    

    Cheers, V.

0 comments:

Post a Comment