Convert string using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert string using sed
# 1  
Old 09-23-2012
Convert string 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 want:

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

Last edited by James Denton; 09-23-2012 at 07:44 PM..
# 2  
Old 09-23-2012
Think someone has already asked for this:

https://www.unix.com/shell-programmin...urce-file.html
# 3  
Old 09-23-2012
Thanks

Thanks!!
# 4  
Old 09-23-2012
If you're going to ask for help with your homework, there's a homework forum with additional posting requirements. Read the FAQ.
https://www.unix.com/homework-coursew...ons-forum.html

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a String to a Number

I read in two numbers from a user but the number is a string. #!/bin/bash read -p "Enter first number: " num1 read -p "Enter second number: " num2 I know you can use the the "expr" or "bc" command to automatically convert the string to a number then add them together. But I don't want to add... (10 Replies)
Discussion started by: Loc
10 Replies

2. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

Help with convert string

Hi. I will be very appreciated for help. I need replace all characters into string with \ (backslash) I mean if I have word abcdefg as input. How I can convert it to \a\b\c\d\e\f\g Thanks and best regards. Staas. (5 Replies)
Discussion started by: beckss
5 Replies

5. UNIX for Dummies Questions & Answers

sed help - convert a string of form ABC_DEF_GHI to AbcDefGhi

How can covert a string of form ABC_DEF_GHI to AbcDefGhi using any online command such as sed etc. ? Thanks. (3 Replies)
Discussion started by: frozensmilz
3 Replies

6. Shell Programming and Scripting

Convert output to string

Hi Guys, is there any command to convert the output returned by the below command to string format: Code: sed 1!d filename Output is : 108 ---------- Post updated at 11:03 AM ---------- Previous update was at 11:00 AM ---------- Because i am using this output as string parameter ... (4 Replies)
Discussion started by: kam786sim
4 Replies

7. UNIX for Dummies Questions & Answers

Convert string to uppercase

i have this piece of small code that checks for *.CSV files. NUMFILES=`ls -1 *.CSV | wc -l` for filename in $(ls -1 *.CSV) do ... done it works only if the files has an uppercase of *.CSV extension. however, when there is a file of the same type but has lowercase *.csv... (1 Reply)
Discussion started by: wtolentino
1 Replies

8. Shell Programming and Scripting

how to convert array into the string

Hi I have a line/string as follows: A=" 3498 NORDEA - INDX LINKED NORY" which was converted into an array of characters: p321$ echo "${ARR}" 3 4 9 8 N O R D E A - I N D X L I N K E D N O R Y When I am trying print this array there are blank... (4 Replies)
Discussion started by: aoussenko
4 Replies

9. Shell Programming and Scripting

how to convert a string to int

Hi, i want to read a text file whose content(single line) will be a number like 1 or 2 or 3 ..... what i want to do is to read the file and increment the content of the file, using unix scripting. Regards, Senthil Kumar Siddhan. (2 Replies)
Discussion started by: senthilk615
2 Replies

10. Shell Programming and Scripting

Convert string to numeric

Hi, I have read some figures from a text file by getting the position and wish to do some checking, but it seem like it won't work. eg. my figure is 0.68 it still go the the else statement, it seems like it treat it as a text instead of number. Anybody can Help ? Thanks. # only... (3 Replies)
Discussion started by: kflee2000
3 Replies
Login or Register to Ask a Question