我正在使用一个python脚本,它基于完全正常的坏测试证书输出许多警告/错误(转到stderr)。基于几个SO帖子,我找到了一种运行脚本并忽略select stdout、stderr行的方法,但它很麻烦:
runThing 3>&1 1>&2 2>&3 3>&- | grep -r 's/Insecure/'
OR
runThing 3>&1 1>&2 2>&3 3>&- | sed 's/Insecure/g'
两者都会过滤掉很多行,比如:
/Users/xxx/.blah/lib/python2.7/site- packages/requests/packages/urllib3/connectionpool.py:791: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html InsecureRequestWarning)
但是sed有这个错误: sed:1:“s/unsecure/g”:正则表达式中未终止的替换
在经常使用的一行末尾添加了很多内容(真正的runThing有命令和参数),因此我尝试生成如下命令:
runThingClean() { command runThing "$@" > /dev/null 3>&1 1>&2 2>&3 3>&- | sed 's/Insecure/g' & }
当我运行这个时,它现在无法过滤(并显示错误是sed):
sed: 1: "s/Insecure/g": unterminated substitute in regular expression
有人能帮我修改这个命令吗?你知道吗
提前谢谢。。你知道吗
该错误意味着您没有正确关闭
sed
中的s
。对于s
,它应该是s/regex/substitution/g
如果您试图删除
Insecure
或替换:如果试图仅显示带有
Insecure
的行:完全不要这样做:只要告诉Python不要首先打印警告,然后就不需要过滤掉它们。你知道吗
或者,考虑修改调用相关组件的代码,以抑制调用站点上的警告,如given in the Python documentation:
最后,如果你真的想把它作为一个过滤器来处理,只需要使用
grep -v Insecure
;sed
就可以了。你知道吗所有这些重定向所做的就是在grep之前和之后交换stdout和stderr两次以恢复它们。让我们看看它是如何工作的:
相关问题 更多 >
编程相关推荐