pagerbak
pagerbak

Reputation: 93

sed and capture

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,

  1. $
  2. {
  3. Zero or more characters not }
  4. }

but clearly I'm not or perhaps I'm missing something else.

Upvotes: 1

Views: 126

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions