user2851669 user2851669 - 1 year ago 69
Bash Question

Replacing $a$ to &a using shell script

I want to replace all the occurrences of

using shell script. For example,
should become

o/P - &HELLO

I tried using sed to replace the string.

sed -i -e 's/$*$/&/g filename


But I am unable to make sure that the last $ is replaced by a blank space and not

Answer Source

Using grouping and backreference::

$ sed 's/\$\([a-zA-Z0-9]*\)\$/\&\1/g' <<< '$HELLO$'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download