如何正确地将值100的参数`q`传递给我的sed表达式? [英] How can I pass correctly the parameter `q` with value 100 to my sed expression?
问题描述
我正在尝试了解关于sed的返回码(不匹配)的答案,但是表达式 sed'/foo/{s/f/b/; q}毫无意义./foo/!{q100}'
.他不能创造一个简单的例子吗?为什么会有这么多的斜杠/
,分号;
和额外的一对/foo/
.
I am trying to understand the answer on Return code of sed for no match, but it dos not make any sense the expression sed '/foo/{s/f/b/;q}; /foo/!{q100}'
. He could just not create a simple example? Why these so many slashes /
, semicolons ;
and extra pair of /foo/
.
我试图用 foo
替换 tr
,并在成功执行操作时使sed的返回码 $?$
为100.我尝试了此代码,但无法正常工作.
I am trying to replace the tr
by foo
and make sed have the return code $?
of 100 when its succeeds the operation. I tried this code and does not work.
echo "trash"|sed 's/tr/foo/!{q100}'; echo $?
输出为:
sed: -e expression #1, char 10: unknown option to `s'
1
如何正确将值100的参数 q
传递给sed表达式?
How can I pass correctly the parameter q
with value 100 to my sed expression?
推荐答案
您的sed命令错误;这样就可以了:
Your sed command is wrong; this does the job:
$ echo trash | sed '/tr/{ s/tr/foo/; q100 }'
fooash
$ echo $?
100
sed命令的作用:
/tr/ { # find the tr string, then enter the block
s/tr/foo/; # replace tr with foo
q100 # exit with 100
}
不幸的是,众所周知, sed
是一种加密语言.所有的斜杠和分号都必须先用眼球解密.我强烈建议您在深入学习之前,先阅读一个完整的sed教程: http://www.grymoire.com/Unix/sed.html
Unfortunately, sed
is well known to be an encrypted language. All the slashes and semicolons need to be decrypted first with eyeballs. I highly recommend you to follow a good and thorough sed tutorial before stepping into the deep water: http://www.grymoire.com/Unix/sed.html
这篇关于如何正确地将值100的参数`q`传递给我的sed表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!