ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have code that currently uses the function:
void getMyTime(MyTimeStruct*);
and i would like to replace all calls to this function to calls to a different function with a prototype:
MyTimeStruct newGetMyTime(void);
For example, i would like the following line:
getMyTime(&startTime);
to be changed to:
startTime = newGetMyTime();
You could try using something like SED on each of your files that has that function in it. Your request seems simple enough that beginner SED tutorials on Google should get you where you need to be. If I remembered the syntax I would write it out.
You can do this in one statement using some combination of cat, grep & sed, with sed being the main ingredient (and sed probably can do it all by itself, without grep or cat).
If you're doing a large global replace in a lot of files, be careful - what if the string you are replacing is a sub-string of another function name which you do not want to modify? Check first.
Perl has some nice regular expression extensions which can help, e.g. the \b pattern (word boundary), and you can user Perl on the command like much like sed and friends., e.g.
Code:
perl -i.original -npe 's/\bmain\b/MAIN/g' *.c
...will replace all instances of the whole word "main" with "MAIN" in all files whose name ends in ".c", saving the original version of the file with the name suffix ".original" (e.g. test.c becomes test.c.original).
Oh, for removing the stuff between the brackets, you need to add a little something. Don't forget to escape the brackets with a \ character since they are special characters in regular expressions, i.e. the Perl code is like this:
You can look in the perlrun manual page to see what the -i, -n, -p, and -e options do. The Perl program (the bit in the single quotes) consists of a single substitution operation. The syntax is
Code:
s/replacethis/withthis/flags;
Where:
replacethis is a Perl-style regular expression pattern to search for (see perlre manual page),
withthis is what to replace found-patterns with.
flags is a list of single character options which modify the behaviour of operation. "g" means global - replace all instances of the pattern on a given input line instead of just the first match, which is the default behaviour.
A more detailed description of the substitution operator can be found in the perlop manual page.
Based on angrybanana's sed command i created a substitution that only affects getMyTime function.
Code:
$ cat a
getMyTime(&startTime);
setMyTime(&startTime);
$ cat s
s/getMyTime(&\(.*\))/\1 = newGetMyTime()/
$ sed -f s a
startTime = newGetMyTime();
setMyTime(&startTime);
I had to put the command in a script file because i am running under cygwin and the ampersand character (&) in the regular expression is confusing the shell. Any way to use ampersand in regular expressions in the shell?
I had to put the command in a script file because i am running under cygwin and the ampersand character (&) in the regular expression is confusing the shell. Any way to use ampersand in regular expressions in the shell?
Dunno much about cygwin, but you could try double quotes (") or backslash (\).
This form doesn't work in cygwin. I get the following error message:
Code:
sed: -e expression #1, char 12: unterminated `s' command
'\' is not recognized as an internal or external command,
operable program or batch file.
Quote:
Originally Posted by ntubski
Code:
$ sed "s/getMyTime(\&\\(.*\\))/\\1 = newGetMyTime()/" a.c
startTime = newGetMyTime();
setMyTime(&startTime);
This form works in cygwin! Thanks for the tip.
I was wondering if there could be a more general regular expression that would substitute for all possible c forms of the statement. For example, include also the following cases:
getMyTime (&startTime); // Spaces between function name and parenthesis
getMyTime(
&startTime
)
; // Statement broken into several lines. This is a legal c statement.
This form doesn't work in cygwin. I get the following error message:
Maybe you miscopied the command? I actually tested both commands in cygwin before posting.
Quote:
Originally Posted by zvirack
I was wondering if there could be a more general regular expression that would substitute for all possible c forms of the statement. For example, include also the following cases:
1. getMyTime (&startTime); // Spaces between function name and parenthesis
2. getMyTime(
&startTime
)
; // Statement broken into several lines. This is a legal c statement.
sed is a line based tool, it might be possible to do this with pattern spaces or something but I'm not sure. Just spaces is easy though:
Code:
$ sed 's/getMyTime[ ]*([ ]*&[ ]*\(.*\)[ ]*)/\1 = newGetMyTime()/' a.c
startTime = newGetMyTime(); // Spaces between function name and parenthesis
getMyTime(
&startTime
)
; // Statement broken into several lines. This is a legal c statement. Not changed by the sed regexp though :(
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.