sed 错误:“'s' 命令的 RHS 上的引用无效 1" [英] sed error: "invalid reference 1 on `s' command's RHS"

查看:41
本文介绍了sed 错误:“'s' 命令的 RHS 上的引用无效 1"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 为 maven 着色脚本 的核心,我运行了几个替换命令.sed 命令之一使用正则表达式,它可以在 shell 中找到作为 在这里讨论.可以在此处找到当前(不工作)的实现.

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:

变体 1:

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

适应脚本:

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

错误: shell 输出的信息与我输入 $ sed 的信息相同.奇怪!?

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

变体 2:

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

适应脚本:

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

错误:

sed: -e 表达式 #7, char 59: `s' 命令的 RHS 上的无效引用 1

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

推荐答案

您不需要实际捕获才能使其工作吗?即对于变体#2:

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" 

(注:未经测试)

如果没有 -r 参数,反向引用(如 1)将不起作用,除非每个括号都用 字符转义.

Without the -r argument back-references (like 1) won't work unless each parenthesis is escaped with a character.

使用 -r,参数反向引用(如 1)将不起作用,除非括号没有被转义.

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

这篇关于sed 错误:“'s' 命令的 RHS 上的引用无效 1"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆