3

長いCファイル内に構造体ブロックがあります

struct node {
   int val;
   struct node *next;
};

sed 関数を使用してこの構造体ブロックを見つけて 1 行に変換するにはどうすればよいですか。したがって、次のようになります。

struct node {   int val;   struct node *next;};

前もって感謝します


私の入力はこれです:

struct node {
   int val;
   struct node *next;
};

typedef struct {
   int numer;
   int denom;
} Rational;

int main()
{
struct node head;
Rational half, *newf = malloc(sizeof(Rational));

head = (struct node){ 5, NULL };
half = (Rational){ 1, 2 };
*newf = (Rational){ 2, 3 };
}

私の出力は次のとおりです。

struct node { int val; struct node *next;};

typedef struct { int numer; int denom;} Rational;int main(){struct node head;Rational  half, *newf = malloc(sizeof(Rational));head = (struct node){ 5, NULL };
half = (Rational){ 1, 2 };
*newf = (Rational){ 2, 3 };
}

struct node { int val; struct node *next;};
struct node:と typedef struct:typedef struct { int numer; int denom;} Rational; を 1 行にまとめたいだけです。ただし、int main() は Rational の末尾に追加されています。

メイン関数の内容はそのまま残してほしい。

4

1 に答える 1

3

sedを使用:

sed '/struct[^(){]*{/{:l N;s/\n//;/}[^}]*;/!t l;s/  */ /g}' input.c

sed構造体定義 ( )が表示されると、改行も削除しながら、行 ( ) に a が表示さ/struct[^{]*{/れるまで行を読み取ります。一致すると、余分なスペース ( ) が削除されます。};:l N;s/\n//;/[}];/!t l;};;s/ */ /g

于 2012-09-19T20:01:00.847 に答える