2011-10-11 30 views
0

對此問題已有嘗試,Detecting and concatenating a multi-line structure using sed,但沒有任何答案適用於我的特定情況。將結構定義壓縮成一行,並使用sed製作兩個副本

我有一個用c編寫的程序,它有兩種多線結構。我需要弄清楚如何使用unix實用程序sed將這些結構定義壓縮到一行,然後創建兩個副本,因此總共有三個結構定義副本。

這裏是我的C程序示例:

#include <stdio.h> 
#include <stdlib.h> 

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 }; 

    return 0; 
} 

我知道,sed命令將尋找東西的sed '/ *struct .*/N;' test.c程度,找到結構,然後一個分支語句上的行的其餘部分追加結構定義爲單行。

+1

http://www.grymoire.com/Unix/Sed.html –

+0

困難的部分是檢測struct * ends *;這是一個C語法分析問題,對於sed來說很重要。你爲什麼要做這個可怕的事情呢? – Beta

+0

這是一個家庭作業問題。最終目標是讓程序爲c89編譯,所以我必須更改結構聲明。 –

回答

0

下面介紹如何操作。有一個文件調用sed -f,然後用sed命令列表創建一個文件。該文件如下。

/typedef struct/b a 
/^struct.*{/!b;:a;N 
/}/!b a;p 
s/\n//g 
相關問題