sed错误"\ 1未在RE中定义".在MacOSX 10.9.5上 [英] Sed error "\1 not defined in the RE" on MacOSX 10.9.5

查看:256
本文介绍了sed错误"\ 1未在RE中定义".在MacOSX 10.9.5上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用bash为我的MP3文件名(非常重要)构建通用格式化程序,其中很大一部分是能够使用正则表达式变量来移动文本.例如,我正在尝试删除英尺(Kevin Parker)周围的括号().

I am trying to build a generic formatter for my MP3 file names (very important) with bash, and a large part of this is being able to move text around using regex variables. For example I am trying to remove the parentheses () from around the ft. Kevin Parker.

oldfilename="Mark Ronson - 02 Summer Breaking (ft. Kevin Parker).mp3"

newfilename=$(echo $oldfilename | sed -E "s/ft.\(*\)/ft.\1/g")

这会导致错误:

sed: 1: "s/ft.\(*\)/gt.\1/g": \1 not defined in the RE

我尝试转义而不转义(),并按照

I have tried escaping and not escaping the (), and adding and removing the -E switch as recommended by .bash_profile sed: \1 not defined in the RE. Help?!

推荐答案

如果使用-E,则\(\)是实际的括号;要捕获,只需使用().在这里,您想删除括号,因此需要匹配文字(,捕获直到下一个)的内容,然后匹配但不捕获结束的),仅用捕获替换整个批次:

If you use -E, then \( and \) are actual parentheses; to capture, you'd use just ( and ). Here, you want to remove parentheses, so you need to match a literal (, capture the content up to the next ) and match but not capture the close ), and replace the whole lot with just the capture:

newfilename=$(echo "$oldfilename" | sed -E "s/\((ft[^)]*)\)/\1/g")

或者,为了获得娱乐价值,您可以不使用-E来做到这一点:

Or, for amusement value, you can do it without -E:

newfilename=$(echo "$oldfilename" | sed -e "s/(\(ft[^)]*\))/\1/g")

(-e是一个作弊项;它只是标识了sed脚本一部分的表达式.这并不意味着与-E相反",并且您可能同时拥有-E和一个或多个-e …arg…参数对.)

(The -e is a cheat; it just identifies an expression that's part of the sed script. It does not mean 'opposite of -E' and you could have both -E and one or more -e …arg… argument pairs.)

请注意,除非故意确保删除任何前导或尾随空格,并且将所有内部选项卡或换行符替换为空白,并且名称中的任何多个空格替换为单个选项卡,否则文件名都应使用引号引起来. .如果您确实想要空间归一化",那么最好不要使用引号.

Note that the file name should be in quotes unless you are deliberately ensuring that any leading or trailing blanks are removed, and any internal tabs or newlines are replaced with blanks, and any multiple blanks in the name are replaced with a single tab. If you do want the 'space normalization', then leaving the quotes out is better.

这篇关于sed错误"\ 1未在RE中定义".在MacOSX 10.9.5上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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