#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Convert strings using sed


    I have a couple structure definitions in my input code. For example:

    Code:
    struct node {
       int val;
       struct node *next;
    };
    or

    Code:
    typedef struct {
       int numer;
       int denom;
    } Rational;
    I used the following line to convert them into one line and copy it twice.

    Code:
    sed '/struct[^(){]*{/{:l N;s/\n//;/}[^}]*;/!t l;s/  */ /g;p;p}'
    The result is this:
    Code:
    struct node { int val; struct node *next;};
    struct node { int val; struct node *next;};
    struct node { int val; struct node *next;};
    
    typedef struct { int numer; int denom;} Rational;
    typedef struct { int numer; int denom;} Rational;
    typedef struct { int numer; int denom;} Rational;
    This is what I would like:

    I want sed commands that takes an input file that contains those lines(repeating struct definitions) and does the following.

    1. I would like the first line to be restored to the original structure block
    2. I would like the second line to turn into to a function heading that looks like this...

    Code:
      void init_structName( structName *var, int data1, int data2 )
    -structName is basically the name of the structure.
    -var is any name you like.
    -data1, data2.... are values that are in the struct.

    3. I would like the third line to turn into to the function body. Where I initialize the the data parameters. It would look like this.

    Code:
        {
            var->data1 = data1;
            var->data2 = data2;
        }

    So my input initially consists of struct definitions. After running the sed command shown above, all of them are converted into one line and copied twice.

    This is the input we need to work with. So when the code finds a structure defintion it can assume that there will be two more copies below.

    For example, this is the output I want if the input file had the repeating lines shown above(node and rational).

    Code:
    struct node {
       int val;
       struct node *next;
    };
    void init_node(struct node *var, int val, struct node *next)
    {
    var->val = val;
    var->next =  next;
    }
    
    typedef struct {
       int numer;
       int denom;
    } Rational;
    void init_Rational( Rational *var, int numer, int denom ) 
    {
       var->numer = numer;
       var->denom = denom;
    }
    In case someone was curious. These functions will be called from the main to initialize the struct variables.

    If you want clarification please ask, I will respond quickly!!

    Can someone help? Thanks so much!!
    -James D
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    If all the output files will look like the below except for the variable assignments, Why not create a template file that looks like that and just change the name on the vaiable assignments:
    Code:
    struct node {
       int val;
       struct node *next;
    };
    void init_node(struct node *var, int val, struct node *next)
    {
    var->val = val;
    var->next =  next;
    }
    
    typedef struct {
       int numer;
       int denom;
    } Rational;
    void init_Rational( Rational *var, int numer, int denom ) 
    {
       var->numer = numer;
       var->denom = denom;
    }

IMN logo majestic logo threadwatch logo seochat tools logo