2012-07-05 42 views
0

我有一個包含在C.如何用多行模式匹配圖案並顯示它們?

結構的定義中的頭文件(.h)中有些是這樣定義:

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

一些定義是這樣的:

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

和一些命令結構定義: 有些定義如下:

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

如何使用egrep或更多的unix命令來查找頭文件中的所有結構並打印結構的所有名稱?

Thanx。

+0

嗅嗅...功課? – Didaxis 2012-07-05 20:26:15

+0

不是作業,是我個人工作的一部分,它有助於檢測.h文件中定義的所有結構的大小更改。 – bsb3166 2012-07-05 20:33:41

+0

你考慮過ctags嗎? – Jack 2012-07-05 20:35:05

回答

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

+0

謝謝lgor。修改爲cat /tmp/1.txt | perl -e'local $ /; $ _ = <>;打印$ 1。「------ \ n」while(/(^ typedef struct [a-zA-Z _] * {。*?}。*?\ n)/ msg);'爲我工作。那麼什麼命令可以幫助只提取結構的名稱? – bsb3166 2012-07-05 20:56:40

+0

增加了對結構名稱的解釋 – 2012-07-05 21:35:07