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

I run several substitution commands as the core colorize script for maven . One of the sed commands uses a regex that works in the shell as discussed here . The current (non-working) implementation can be found here .

When I include one of the command options in the script, another behavior occurs:

Option 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 displays the same information as if I typed $ sed . Strange !?




Option 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" \ 

Mistake:

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

+49
regex sed substitution
May 19 '13 at 18:06
source share
3 answers

Don't you really need to grab it to work? i.e. for option 2:

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

(Note: untested)

+15
May 19 '13 at 18:10
source share

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 

Backslashes in front of each bracket should be avoided:

 /^$/b :loop $!{ N /\n$/!b loop } s/\n\(.\)/\1/g 
+32
Jan 18 '16 at 22:34
source share

You need to exit / after .

 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" 
+4
May 19 '13 at 18:15
source share



All Articles