将结构定义压缩为一行并使用 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 查找结构,然后使用分支语句将结构定义的其余行附加到单行。
There has been an attempt at this question already, Detecting and concatenating a multi-line structure using sed, but none of the answers worked for my specific case.
I have a program written in c which has two kinds of multi-lined structures. I need to figure out how to use the unix utility sed to compress these structure definitions to a single line and then make two copies, so there are three copies of the structure definition in total.
Here is my c program example:
#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;
}
I know that the sed command will look something to the extent of sed '/ *struct .*/N;' test.c
to find the structure and then a branch statement to append the rest of the lines of the structure definition to the single line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是具体操作方法。有一个调用 sed -f 的文件,然后创建一个包含 sed 命令列表的文件。该文件如下。
Here's how to do it. Have a file which calls sed -f and then create a file with a list of sed commands. The file goes as follows.