sed-用两个变量的乘积替换幂2的变量 [英] sed - replace a variable of power 2 by the product of two variables

查看:61
本文介绍了sed-用两个变量的乘积替换幂2的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 sed 替换以下表达式:

I would like to replace with sed the following expression :

cos(2*a)^2+sin(3*b)^2+m1^2*m2^2*cos(4*c) 

通过

cos(2*a)*cos(2*a)+sin(3*b)*sin(3*b)+m1*m1*m2*m2*cos(4*c) 

如您所见,我想通过这些变量本身的乘积来转换所有幂为2(具有^ 2指数)的变量.

As you can see, I would like to convert all variables with power 2 (with ^2 exposant) by the product of these variables by themselves.

我尝试使用符号"^ 2"之前的模式进行操作:

I tried to do it with pattern preceding the symbol "^2":

echo "cos(2*a)^2+sin(3*b)^2+m1^2*m2^2*cos(4*c)"  | sed 's/\(.*\)\^2/\1\*\1/g'

但它不起作用.

如果有人可以帮助我...

If someone could help me ...

谢谢

推荐答案

您可以玩角色类:

sed 's/\([^(*+\/^-]*\(([^)]*)\)\?\)\^2/\1\*\1/g'

请注意,如果您嵌套了括号,这将停止工作.

Note that this will stop to work if you have nested parenthesis.

这篇关于sed-用两个变量的乘积替换幂2的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆