user1684072
user1684072

Reputation: 169

String conversion with sed

I have a couple lines in my input where I am initializing structs. Everytime I see something like the following line in the input file:

something1 = (struct something2){ something3, something4};

I need to convert it to:

init_something2( &something1, something3, something4);

I used the following function and it works.

sed -e 's/\([a-zA-Z0-9]*\)\s*=\s*(\s*struct\s\([a-zA-Z0-9]*\)\s*)\s*{\s*\([a-zA-Z0-9]*\)\s*,\s*\([a-zA-Z0-9]*\)\s*}\s*;/init_\2( \&\1, \3, \4);/g'

My question is how do I modify it so it will work for however many inputs. Can you modify it to do any number of inputs. Like for example:

something = ( struct something2) {something3, something4, something5, something6..};

should become

init_something2( &something1, something3, something4, something5, something6..);

(Pay attention to the paranthesis vs braces) Thanks a lot!

Upvotes: 0

Views: 58

Answers (1)

nneonneo
nneonneo

Reputation: 179392

Sure. Just use ([^}]*)} to capture everything before the brace, e.g.

sed -e 's/\([a-zA-Z0-9]*\)\s*=\s*(\s*struct\s\([a-zA-Z0-9]*\)\s*)\s*{\([^}]*\)}\s*;/init_\2( \&\1, \3);/g'

Upvotes: 1

Related Questions