I'm trying to use find + sed/awk to replace this string:
main(argc, argv)
int argc;
char *argv[];
{
with:
int main(int argc, char *argv[])
{
Does anyone know how to do this?
Many thanks!
I'm trying to use find + sed/awk to replace this string:
main(argc, argv)
int argc;
char *argv[];
{
with:
int main(int argc, char *argv[])
{
Does anyone know how to do this?
Many thanks!
cat test.txt
main(argc, argv)
int argc;
char *argv[];
{
with this awk it produce the desired output:
cat test.txt | awk 'BEGIN { args=""; nb=0; }; /main\(argc, argv\)/,/\{/ { if (!/^(main|\{).*$/) { tmp=$0; sub(/;/,"",tmp); if (nb) { args=(args "," tmp); nb+=1; } else { args=tmp; nb+=1;} };} END { print "main(" args ")\n{\n"; }'
Output:
main(int argc,char *argv[])
{
Details on the awk part:
BEGIN { args=""; nb=0; }; # Init Variables
/(main\()argc, argv\)/,/\{/ { # for lines matching 'main' to line matching '{'
if (!/^(main|\{).*$/) { # if we're not on the main or { line
tmp=$0; # copy the match
sub(/;/,"",tmp); # remove the trailing ;
if (nb) { # if we're not on the first arg, concat with preceding separed by ,
args=(args "," tmp);
nb+=1;
} else { # it is the first arg, just assign
args=tmp;
nb+=1;
}
};
}
END { print "main(" args ")\n{\n"; } # we finished matching, print the result.
This is just on your exemple, if you need something more complex you should be able to use a match group and $1 $2 for the function name if needed.
Edit: I let you extend it for you particular needs in replacement within files.