这很容易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
中。