Sed error “\1 not defined in the RE” on MacOSX 10.9.5

随声附和 提交于 2020-03-03 06:07:24

问题


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")

This causes the error:

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?!


回答1:


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")

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

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

(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.



来源:https://stackoverflow.com/questions/28072190/sed-error-1-not-defined-in-the-re-on-macosx-10-9-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!