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

RegexSedSubstitution

Regex Problem Overview


I run several substitution commands as the core of a colorize script for maven. One of the sed commands uses a regular expression which works find in the shell as discussed here. The current (not working) implementation can be found here.

When I include one of the variants of the command into the script different behavior occurs:

Variant 1:

$ sed -re "s/([a-zA-Z0-9./\\ :-]+)/\1/g"

Adapted to the script:

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

Error: The shell outputs the same information as if I would type $ sed. Strange!?


Variant 2:

$ sed -e "s/\([a-zA-Z0-9./\\ :-]\+\)/\1/g"

Adapted to the script:

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

Error:

> sed: -e expression #7, char 59: invalid reference \1 on `s' command's RHS

Regex Solutions


Solution 1 - Regex

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 unless each parenthesis is escaped with a \ character.

With -r, argument back-references (like \1) won't work unless the parenthesis are NOT escaped.

Solution 2 - Regex

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

Solution 3 - Regex

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

Solution 4 - Regex

You need escape the / after the .

sed -e "s/\([a-zA-Z0-9.\/\\ :-]\+\)/\1/g"

Or if you don't want to worry about escaping, use |

sed -e "s|\([a-zA-Z0-9./\\ :-]\+\)|\1|g"

EDIT:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJJDView Question on Stackoverflow
Solution 1 - RegexDenis de BernardyView Answer on Stackoverflow
Solution 2 - Regexe18rView Answer on Stackoverflow
Solution 3 - RegexOrangeDogView Answer on Stackoverflow
Solution 4 - RegexslackmartView Answer on Stackoverflow