I am writing a c++ program that has many calls of pow(input,2). I now realize that this is slowing down the program and these all should be input * input for greater speed.
There should be a simple way of doing this replacement throughout my file with awk, but I am not very familiar with awk. Can anyone help me out?
To be clear, I want
pow(input,2)
replaced with
input * input
throughout the entire file. What I detonated as input varies greatly through all of my pow calls.
I know
awk '{gsub(/foo/,"bar");print}'
Will do search and replace but I'm not entirely sure how I would save the input string to replace it afterwards.
Hi
If I had understood you correctly:
Assuming a1 is your input file.
# cat a1
Kitten pow(3,2) bc
Dogs pow(4,2) adf
Donkey werwe
Chicken pow(5,2) efg
Turkey werw
#
# sed 's/\(.*\)pow(\([0-9]*\),2)\(.*\)/\1\2\*\2\3/' a1
Kitten 3*3 bc
Dogs 4*4 adf
Donkey werwe
Chicken 5*5 efg
Turkey werw
#
1 Like
Thanks.
That did not entirely work, as in my program it isn't always only numbers inside the pow call, often I'm working with variables as well. However, a very simple change fixed it to work for all cases.
sed 's/\(.*\)pow(\(.*\),2)\(.*\)/\1\2\*\2\3/' a1
I replaced the [0-9] in yours with a . to match any pattern.
Thanks again, I didn't really know how to work the \ \ fields to output what I wanted.