sed error: "invalid reference \1 on `s' command's RHS"

Don't you need to actually capture for that to work? i.e. for variant #2:

-r -e "s/WARNING: (\([a-zA-Z0-9./\\ :-]\+\))/${warn}WARNING: \1${c_end}/g" \

(Note: untested)

Without the -r argument back-references (like \1) won't work.


This error is common for parentheses that are not escaped. Escape them and try again.


For example:

/^$/b
:loop
$!{
N
/\n$/!b loop
}
s/\n(.)/\1/g

Should be escaped with backslashes before each parenthesis:

/^$/b
:loop
$!{
N
/\n$/!b loop
}
s/\n\(.\)/\1/g

If the -r/--regexp-extended option is not provided, then the capturing parentheses must be escaped.