Reputation: 93
I'm trying to get
${test-value} (although I would prefer just test-value)
from this expression
echo 'test=${test-value} ... more text' | sed 's/\${[^}]*}/&/'
but it just prints the input
I think I'm matching like this,
but clearly I'm not or perhaps I'm missing something else.
Upvotes: 1
Views: 126
Reputation: 626845
You match ${test-value}
with \${[^}]*}
, but you replace it with itself as &
in the replacement pattern stands for the whole match value.
Use the following fix if you want to use sed
(to match the whole string but capture what you need and keep it by using a \1
placeholder in the replacement pattern):
sed 's/.*\(\${[^}]*}\).*/\1/'
Or use a matching approach (with grep
using -o
option to extract the matched text):
grep -o '\${[^}]*}'
See an online demo.
Upvotes: 1