0

我有一个头文件(.h),其中包含 C 中结构的定义。

有些是这样定义的:

typedef struct {
 ...
 ...
 ...
} structure1

有些是这样定义的:

typedef struct structure2 {
 ...
 ...
 ...
} structure2

和一些命令的结构定义:一些是这样定义的:

//typedef struct {
// ...
// ...
// ...
//} structure1

如何使用 egrep 或更多 unix 命令查找头文件中的所有结构并打印所有结构的名称?

谢谢。

4

1 回答 1

0

这很容易perl

perl -e 'local $/; $_ = <>; print $1."------\n" while (/(typedef struct {.*?}.*?\n)/msg);'

例子:

$ cat /tmp/1.txt 
typedef struct {
 ...
 ...
 ...
} structure1

hello

typedef struct {
 ...
 ...
 ...
} structure2

bye

$ cat /tmp/1.txt | perl -e 'local $/; $_ = <>; print $1."------\n" while (/(typedef struct {.*?}.*?\n)/msg);'
typedef struct {
 ...
 ...
 ...
} structure1
------
typedef struct {
 ...
 ...
 ...
} structure2
------

已建立的块用---------.

当您只想获取结构的名称时,您必须对正则表达式的另一部分进行分组(使用 对需要的部分进行分组()):

$ cat /tmp/1.txt | perl -e 'local $/; $_ = <>; print $1."\n" while (/typedef struct {.*?}\s*(.*?)\n/msg);'
structure1
structure2

如您所见,我稍微修改了正则表达式:

/typedef struct {.*?}\s*(.*?)\n/

后面的字符串部分}将被捕获到组$1中。

于 2012-07-05T20:34:03.447 回答