0

C の構造体の定義を含む head ファイル (.h) があります。

一部は次のように定義されています。

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

一部は次のように定義されています。

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

いくつかは命令された構造定義です: いくつかは次のように定義されています:

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

egrep または more unix コマンドを使用して head ファイル内のすべての構造を検索し、構造のすべての名前を出力するにはどうすればよいですか?

ありがとう。

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 に答える