我想替换一个由 包围的 var 组成的模式()
。sed
下一个命令成功:
echo "This is the pattern : th_now(5, almost done!" | sed "s/th_now($var/th_now_$var/g"
但是当我尝试使用我感兴趣的正确模式时,它失败了:
echo "This is the pattern : th_now(5), almost done!" | sed "s/th_now($var)/th_now_$var/g"
有办法解决这个问题吗?谢谢。
答案1
如果我理解正确的话,我认为这就是你正在寻找的:
$ echo "This is the pattern : th_now(5), almost done!" | sed 's/th_now(\([^()]*\))/th_now_\1/'
This is the pattern : th_now_5, almost done!
或者:
$ echo "This is the pattern : th_now(5), almost done!" | sed 's/th_now(\([[:digit:]]*\))/th_now_\1/'
This is the pattern : th_now_5, almost done!