I am using OS X 10.10.5 and when I tried the following commands:

$ ls foo*
$ for f in foo*; do echo "$f" | sed -E 's:foo:bar:';done
$ for f in foo*; do echo "$f" | sed -E 's:foo:(&):';done
$ for f in foo*; do echo "$f" | sed -E 's:\(foo\):(\1):';done
sed: 1: "s:foo:(\1):": \1 not defined in the RE

I got an error.

My question is: how to remedy the error yielded by the last command above?


Under -E, you need to use unquoted parentheses to create a capture group.

sed -E 's:(foo):(\1):'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.