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.
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.