Computing.Net > Forums > Unix > Awk and Sed - Line replacement ???

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Awk and Sed - Line replacement ???

Reply to Message Icon

Name: Infinite Recursion
Date: March 8, 2004 at 08:42:28 Pacific
OS: Solaris
CPU/Ram: 2
Comment:

I am trying to replace this line...

strcpy(deletefile_name, string);

with this one ...

strlcpy(deletefile_name,string,strlen(deletefile_name);

Basically, what I am doing is replacing the calls to strcpy with strlcpy (more secure). I have thousands of lines of code and going through them line by line is getting too tedious.

Would someone tell me how I could do this line replacement using Sed, Awk, or Vi? I do not understand regular expressions enough to figure it out.

Any help is greatly appreciated. Thanks in advance!

Sam

(ignore below)
a
ab
abc
abcd
abcde



Sponsored Link
Ads by Google

Response Number 1
Name: aigles
Date: March 8, 2004 at 09:07:24 Pacific
Reply:

sed 's/strcpy(deletefile_name, string);/strlcpy(deletefile_name,string,strlen(deletefile_name);/g' inputfile


Jean-Pierre.


0

Response Number 2
Name: Infinite Recursion
Date: March 8, 2004 at 11:13:42 Pacific
Reply:

Thanks for the quick response Jean-Pierre...

Could you tell me what the syntax would be if the deletefile_name and filename strings were variable? Trying to substitute based on any given instance of strcpy over to strlcpy syntax...

strcpy(variable1,variable2);
to
strlcpy(variable1,variable2,strlen(variable1);

Thank you.

Sam


0

Response Number 3
Name: Infinite Recursion
Date: March 8, 2004 at 11:35:14 Pacific
Reply:

This is what I'm tying to do, based on your previous instructions...

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. Any assistance is greatly appreciated.

Sam


0

Response Number 4
Name: aigles
Date: March 8, 2004 at 11:50:12 Pacific
Reply:

Try this :

sed 's/strcpy(\([^,]\+\),\([^)]\+\));/strlcpy(\1,\2,strlen(\1));/g' input_file


Jean-Pierre.


0

Response Number 5
Name: Infinite Recursion
Date: March 8, 2004 at 12:13:05 Pacific
Reply:

Thanks again for your quick response...
That syntax seems to list out the file's content verbatim with no changes. Any ideas?

Thanks.

Sam


0

Related Posts

See More



Response Number 6
Name: aigles
Date: March 8, 2004 at 13:22:52 Pacific
Reply:

Verify the command you typed.
The command works fine for me :

/home/jp> sed 's/strcpy(\([^,]\+\),\([^)]\+\));/strlcpy(\1,\2,strlen(\1));/g'
strcpy(a,b); strcpy(xx,yy);
strlcpy(a,b,strlen(a)); strlcpy(xx,yy,strlen(xx))
^D
/home/hp>


Jean-Pierre.


0

Response Number 7
Name: Dlonra
Date: March 9, 2004 at 05:33:17 Pacific
Reply:

you did not indicate the number of files you want to modify. Simplest way is
#define strcpy(deletefile_name, string) strlcpy(deletefile_name,string,strlen(deletefile_name)
once at the beginning of each file.

Even manually, this should be fast. If there happens to be a common "include.h", almost instantaneous.


0

Response Number 8
Name: Infinite Recursion
Date: March 9, 2004 at 07:32:33 Pacific
Reply:

Jean-Pierre:

I'm not sure what I'm doing wrong, but it doesn't work out for me...

The data file ...
[comp%] cat test1.dat
data before 1245
strcpy(procfile_name,UPASS_CFG_ENV);
strcpy(deletefile_name, string);
data after 12345

[comp%] sed 's/strcpy(\([^,]\+\),\([^)]\+\));/strlcpy(\1,\2,strlen(\1));/g' test1.dat

data before 12345
strcpy(procfile_name,UPASS_CFG_ENV);
strcpy(deletefile_name, string);
data after 12345

Dlonra:

Thanks for your response. I want to modify one file at a time. In this case, I don't think that the #define approach will work, because the content of the function's parameters are unknown. I would have to manually go through the code, find them all and paste them at the top... redefining them.
It seems to me that I could save time, by modifying them while I am there. I was trying to avoid a manual process. This approach wouldn't be very "fast" in the 15k+ lines of code in each file. I know there is a way in sed or awk to do this in probably less than a line of code, with minimal effort. Still working on that.

Thank you both for your suggestions.

#define strcpy(deletefile_name, string) strlcpy(deletefile_name,string,strlen(deletefile_name)
once at the beginning of each file.
Even manually, this should be fast. If there happens to be a common "include.h", almost instantaneous.



0

Response Number 9
Name: mikeb
Date: March 12, 2004 at 13:22:52 Pacific
Reply:

how about w/ Vi?

:%s/str1/str2/g

Mike



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Awk and Sed - Line replacement ???

Awk: compare and save lines www.computing.net/answers/unix/awk-compare-and-save-lines/6430.html

Increment a number using awk or sed www.computing.net/answers/unix/increment-a-number-using-awk-or-sed/6827.html

replace data in a file www.computing.net/answers/unix/replace-data-in-a-file/6189.html