已经有人尝试过这个问题,Detecting and concatenating a multi-line structure using 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
查找结构,然后使用分支语句将结构定义的其余行附加到单行。
最佳答案
具体操作方法如下。有一个调用 sed -f 的文件,然后创建一个包含 sed 命令列表的文件。该文件如下。
/typedef struct/b a
/^struct.*{/!b;:a;N
/}/!b a;p
s/\n//g
关于将结构定义压缩为一行并使用 sed 制作两个副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7732501/