minil
minil

Reputation: 7135

sed string search and add parameter

I have a C program that calls strlcpy function in 100s of lines.

strlcpy(p->name,getInfo(NULL,&account)); 
strlcpy(p->balance,getInfo(NULL,&account));
strlcpy(p->number,getInfo(NULL,&account)); 
strlcpy(p->address,getInfo(NULL,&account));

I would like to add the sizeof([First Parameter]) as the third parameter to the strlcpy function call using sed. Only the lines with strlcpy should be edited.

Expecting the result as

strlcpy(p->name,getInfo(NULL,&account),sizeof(p->name)); 
strlcpy(p->balance,getInfo(NULL,&account),sizeof(p->balance));
strlcpy(p->number,getInfo(NULL,&account),sizeof(p->number)); 
strlcpy(p->address,getInfo(NULL,&account),sizeof(p->address))

Any thoughts/code is appreciated.

Upvotes: 0

Views: 55

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92874

sed solution:

sed -E 's/^([^(]+)(\([^,]+),([^)]+\)).*/\1\2,\3,sizeof\2));/' file

The output:

strlcpy(p->name,getInfo(NULL,&account),sizeof(p->name));
strlcpy(p->balance,getInfo(NULL,&account),sizeof(p->balance));
strlcpy(p->number,getInfo(NULL,&account),sizeof(p->number));
strlcpy(p->address,getInfo(NULL,&account),sizeof(p->address));

BRE equivalent:

sed 's/^\([^(]*\)\(([^,]*\),\([^)]*)\).*/\1\2,\3,sizeof\2));/' file

Upvotes: 1

Related Questions