Computing.Net > Forums > Programming > Perl : line/string replacement

Perl : line/string replacement

Reply to Message Icon

Original Message
Name: Infinite Recursion
Date: March 8, 2004 at 11:40:38 Pacific
Subject: Perl : line/string replacement
OS: within
CPU/Ram: source code
Comment:


This is what I'm tying to do, replace strcpy with strlcpy syntax for every instance of strcpy within a C file.

Currently, this is what I have in Sed, but it doesn't work (not sure on the regular expressions).

sed 's/strcpy($destination,$source);/strlcpy($destination,$source,strlen($destination);/g' test1.dat

I'm stuck on how I make it process every instance of strcpy. The variables are above... I think I need a regular expression, just not sure on its form.

If anyone can tell me how to do this in Awk, Perl, Vi, Sed or any other unix command line tool... please let me know.

Any assistance is greatly appreciated.

Sam



Report Offensive Message For Removal


Response Number 1
Name: SN
Date: March 8, 2004 at 18:01:50 Pacific
Reply: (edit)

sed 's/strcpy($destination,$source);/strlcpy($destination,$source,strlen($destination);/g' test1.dat

In perl, a correct way to do this would be:
$file =~ s/strcpy\((.*?),(.*?)\)/strlcpy\($1, $2, strlen\($2\)\)/g;

-SN


Report Offensive Follow Up For Removal

Response Number 2
Name: Infinite Recursion
Date: March 9, 2004 at 10:20:34 Pacific
Reply: (edit)

$file =~ s/strcpy\((.*?),(.*?)\)/strlcpy\($1, $2, strlen\($1\)\)/g;

Same as SN's code... just replaced the last $2 with a $1, to have the 1st argument of the function call, be the argument of the strlen function.

IR


Report Offensive Follow Up For Removal

Response Number 3
Name: SN
Date: March 9, 2004 at 13:01:35 Pacific
Reply: (edit)

Whoops...Thanks IR. Good catch.
-SN


Report Offensive Follow Up For Removal

Response Number 4
Name: Infinite Recursion
Date: March 9, 2004 at 13:15:49 Pacific
Reply: (edit)

Thank you both. It works great! :)


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Perl : line/string replacement 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge