如何忽略在stderr中发出的警告

时间:2019-07-09 11:24:56

标签: linux unix terminal

我正在运行命令,我只想在‍ stderr文件中得到错误而不是警告

➜  ~ pip2 install 0wned 2> error.txt 

Collecting 0wned
  Using cached https://files.pythonhosted.org/packages/85/fb/af45132a70fa67b7a40383066e284182ae649ce5c2a7839c06da51641a43/0wned-0.6.0-py2.py3-none-any.whl
Installing collected packages: 0wned

➜  ~ cat error.txt 

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/lib/python2.7/site-packages/0wned-0.6.0.dist-info'
Consider using the `--user` option or check the permissions.

我只想获取error.txt中的错误,而不是警告。

我想要这个输出:

➜  ~ cat error.txt

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/lib/python2.7/site-packages/0wned-0.6.0.dist-info'
Consider using the `--user` option or check the permissions.

1 个答案:

答案 0 :(得分:2)

如果需要,您可以过滤stderr:

 pip2 install 0wned 2> >( grep ERROR > only-errors.txt)

说明:

 2> >( command )

stderr流重定向到command

grepv ERROR > only-errors.txt

将包含ERROR的行复制到only-errors.txt文件中


如果要保留所有错误消息的副本,则可以添加tee

 pip2 install 0wned 2> >( tee all.txt |  grep ERROR > only-errors.txt)