I run the following code
我运行以下代码
sed 's/\([^ ]+\) your \([^ ]+\)/ \2\1er/' < fail
The file fail is
文件失败了
fail your test
The above command gives me
上面的命令给了我
fail your test
although it should give "testfailer".
虽然它应该给“testfailer”。
The second and first globs \2\1
should be at the start of the word "er". This suggests me that the problem may be in the regexes in the search part. However, they seem to be correct for me.
第二个和第一个globs \ 2 \ 1应该在“er”这个词的开头。这表明问题可能出在搜索部分的正则表达式中。但是,它们似乎对我来说是正确的。
Do you see any mistake in the code?
你看到代码中有任何错误吗?
2 个解决方案
#1
Your code does work when you escape the plus signs:
当您逃脱加号时,您的代码可以正常工作:
sed 's/\([^ ]\+\) your \([^ ]\+\)/\2\1er/' < fail
#2
Common or garden variety sed
regex doesn't understand +. Yeah, I know, how stupid is that. So this is an equivalent, working version of your command line:
普通或花园品种sed正则表达不理解+。是的,我知道,那是多么愚蠢。所以这是命令行的等效工作版本:
sed 's/\([^ ][^ ]*\) your \([^ ][^ ]*\)/ \2\1er/' < fail
Also works to request extended regex, in which case you ditch the backslashes on the parens:
也适用于请求扩展正则表达式,在这种情况下你抛弃parens上的反斜杠:
sed -r 's/([^ ]+) your ([^ ]+)/ \2\1er/' < fail
#1
Your code does work when you escape the plus signs:
当您逃脱加号时,您的代码可以正常工作:
sed 's/\([^ ]\+\) your \([^ ]\+\)/\2\1er/' < fail
#2
Common or garden variety sed
regex doesn't understand +. Yeah, I know, how stupid is that. So this is an equivalent, working version of your command line:
普通或花园品种sed正则表达不理解+。是的,我知道,那是多么愚蠢。所以这是命令行的等效工作版本:
sed 's/\([^ ][^ ]*\) your \([^ ][^ ]*\)/ \2\1er/' < fail
Also works to request extended regex, in which case you ditch the backslashes on the parens:
也适用于请求扩展正则表达式,在这种情况下你抛弃parens上的反斜杠:
sed -r 's/([^ ]+) your ([^ ]+)/ \2\1er/' < fail