gpt4 book ai didi

c - 如何将一个模式与多行相匹配并显示出来?

转载 作者:行者123 更新时间:2023-11-30 17:59:32 25 4
gpt4 key购买 nike

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

有些定义如下:

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

有些定义如下:

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

以及一些命令的结构定义:有些定义如下:

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

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

谢谢。

最佳答案

使用 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
------

创建的 block 用---------分隔。

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

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

正如你所看到的,我稍微修改了正则表达式:

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

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

关于c - 如何将一个模式与多行相匹配并显示出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11352048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com