You can take a approach with grep
to find lines that begin with whitespace followed by int
or char
using a basic regular expression with something as simple as:
$ grep '^[ ][ ]*\(int\|char\)' yourfile.c
int x = 99;
int *p1, *p2;
char y[10] = "a"
If you have initial tab
characters, (or mixed spaces
and tabs
), you can use a Perl Compatible Regular Expression:
$ grep -P '^[ \t]+(int|char)' yourfile.c
int x = 99;
int *p1, *p2;
char y[10] = "a"
I don't know if this is exactly what you are looking for, and if not, let me know. There are other c-code scanners that will pull variables, functions, etc out and summarize, e.g. cproto
is one I have used and liked a lot. There are a number of others on sourceforge you may want to check as well.
Explanation (from comment)
Given the example, it was clear that only int
, char
, etc.. were wanted AFTER whitespace (e.g. to miss the int main ()
declaration). With that in mind we set up grep with the basic regular expression to require at least 1
(space
using BRE) or if mixed tab
and space
are possible (using PCRE) before the search term. To handle spaces only, with a BRE:
grep '^[ ][ ]*
or if dealing with mixed spaces and tabs, a PCRE of
grep -P `^[ \t]+
Which anchors the search ^
at the beginning of the line and, with the BRE looks for a space
with the character class [ ]
. To handle at least one space, and for zero
or more additional spaces
, we add an additional [ ]*
.
Where multiple (or mixed) spaces
and tabs
are involved (which cannot be handled easily with BRE), the PCRE essentially does the same thing for both space
and tab
, while sacrificing some portability for the expanded expression handling provided by PCRE. ^[ \t]+
will accommodate one
or more
instances of space
or tab
characters before the search terms. The +
requiring the presence of at least 1
or more matches of the characters within the character class.
When looking for either int
or char
, the basic regular expression format is
\(int\|char\)
where (
and |
do not retain special meaning in a BRE and must be escaped while the PCRE form is simply:
(int|char)
So putting it altogether it simply anchors a search for one-or-more space
(or using PCRE mixed space
and tab
) characters before either int
or char
in making the determination of which lines to display.
Hope that helped.