[英]How to pass output of grep to sed?
我有这样的命令:
cat error | grep -o [0-9]
这是仅打印号码,如2
, 30
等。 现在我希望将这个号码传递给sed
。
就像是 :
cat error | grep -o [0-9] | sed -n '$OutPutFromGrep,$OutPutFromGrepp'
有可能这样做吗?
我是shell脚本的新手。 提前致谢
如果打算打印grep
返回的行,生成sed
脚本可能是要走的路:
grep -E -o '[0-9]+' error | sed 's/$/p/' | sed -f - error
您可能正在寻找xargs
,尤其是-I
选项:
themel@eristoteles:~$ xargs -I FOO echo once FOO, twice FOO
hi
once hi, twice hi
there
once there, twice there
你的例子:
themel@eristoteles:~$ cat error
error in line 123
error in line 234
errors in line 345 and 346
themel@eristoteles:~$ grep -o '[0-9]*' < error | xargs -I OutPutFromGrep echo sed -n 'OutPutFromGrep,OutPutFromGrepp'
sed -n 123,123p
sed -n 234,234p
sed -n 345,345p
sed -n 346,346p
对于实际使用,您可能希望传递sed
输入文件并删除echo
。
(顺便说一句,修正了你的UUOC 。)
是的,您可以将输出从grep传递给sed。
请注意,为了匹配整数,您需要使用[0-9] * *,而不仅仅是[0-9],它只匹配一个数字。
另请注意,您应该使用双引号来扩展变量(在sed参数中),并且您似乎在第二个变量名中有拼写错误。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.