sed中的反向引用返回错误值 [英] Backreferences in sed returning wrong value

查看:99
本文介绍了sed中的反向引用返回错误值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sed替换表达式.正则表达式只能在vim中使用,而不能在sed中使用.我用斜杠代替数字前的最后一个破折号,

I am trying to replace an expression using sed. The regex works in vim but not in sed. I'm replacing the last dash before the number with a slash so

/www/file-name-1 

应该返回

/www/file-name/1

我正在使用以下命令,但它会继续输出/www/file-name/0

I am using the following command but it keeps outputting /www/file-name/0 instead

sed 's/-[0-9]/\/\0/g' input.txt

我在做什么错了?

推荐答案

您必须将括号括在数据之间以供以后引用,并且sed开始计入1.要恢复所有不需要括号的匹配字符,它使用&符号.

You must surround between parentheses the data to reference it later, and sed begins to count in 1. To recover all the characters matched without the need of parentheses, it is used the & symbol.

sed 's/-\([0-9]\)/\/\1/g' input.txt

结果是:

/www/file-name/1

这篇关于sed中的反向引用返回错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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