[英]Replacing $a$ to &a using shell script
I want to replace all the occurrences of $[az|AZ|0-9]$
to &[az|AZ|0-9]
using shell script. 我想使用shell脚本将
$[az|AZ|0-9]$
所有出现替换为&[az|AZ|0-9]
。 For example, $HELLO$
should become &HELLO
. 例如,
$HELLO$
应该变成&HELLO
。
I/P - $HELLO$
o/P - &HELLO
I tried using sed to replace the string. 我尝试使用sed替换字符串。
sed -i -e 's/$*$/&/g filename
I/P - $HELLO$
O/P - &HELLO&
But I am unable to make sure that the last $ is replaced by a blank space and not &
但是我无法确保最后的$被空格代替,而不是
&
Using grouping and backreference:: 使用分组和反向引用:
$ sed 's/\$\([a-zA-Z0-9]*\)\$/\&\1/g' <<< '$HELLO$'
&HELLO
Solution using awk
: This uses awk's inbuilt function sub
to search and replace $
sign and then sub
is again used to replace $
to &
. 使用
awk
解决方案:这使用awk的内置函数sub
来搜索和替换$
符号,然后再次使用sub
来将$
替换为&
。
echo '$HELLO$' |awk '{sub(/\$/,"\\&");sub(/\$/,"")}1'
&HELLO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.