Re: sed to grep a "typedef struct { (multiple lines) } TYPE;



kolmogolov@xxxxxxxxx wrote:
Thanks for the awk and perl answers too!
I think I'm learning the smaller sed in the first place. :)

Steffen Schuler 寫道:


kolmogolov@xxxxxxxxx wrote:

Hi,

please help with greping a multiple-line instance
of a typedef struct in C such as

typedef struct {
line1;
line2;
(variable number of lines)
line n;
} TYPE;


sed -n '/typedef[ \t][ \t]*struct[ \t]*{/,/}/p' FILE


And what is now in the pattern space? I suppose

^typedef.*TYPE;$

right? But with or without '\n'? Whay does

sed -n '/typedef[ \t][ \t]*struct[ \t]*{/,/}/s/\(^.*TYPE.*$\)/\1/p'
FILE

print only

} XSizeHints;

?

Hmmm... Could it be that the pattern space contains
only the last line "} TYPE;" when I search for TYPE?
If this is the case, it would be difficult to select only
the definition of TYPE for printing. How do I accumulate
every line in this address range to the hold space and
print all of them only when the last line contains TYPE?


but this one-liner doesn't observe comments, string-, and char-literals,
nested braces, an identifier with suffix typedef, or a struct label. A
correct script needs a partial C-parser.


Thanks for the hint too. I think this would be useful enough
for the moment come what may in the future.
I just need some handy way for looking up definitions
of large structures.


Then use cscope. "cscope" builds a symbol database from a list of source files and provides a textual (curses-based) interface where you can search code for:

# all references to a symbol
# global definitions
# functions called by a function
# functions calling a function
# text string
# regular expression pattern
# a file
# files including a file

Its GUI front-end "cbrowser" provides a hierarchical function call tree
plus other features.

cscope is at http://cscope.sourceforge.net/
cbrowser is at http://cbrowser.sourceforge.net/

There are other text-based tools that post-process the database that
cscope builds, e.g. "ccalls" (to print function call pairings) and "dt"
(to take those pairings and produce a function call hierarchy) but I
don't know of a free download for those. You can get ccalls at least for
a small charge from
http://www.bell-labs.com/project/wwexptools/paypackages.html

Ed.
.



Relevant Pages