Piping error messages with grep

試著忘記壹切 提交于 2020-01-04 07:53:11

问题


I have a script that fails because some files are missing.

Running the script and piping it to grep

$ ./adder | grep Error

produces the following output:

Error in <TFile::ReadBuffer>: error reading all requested bytes from file v2.2_V3_194424_194712/output_853.root, got 0 of 300
Error in <TFile::Init>: v2.2_V3_194424_194712/output_853.root not a ROOT file

and similar output with different files

I'd like to extract the root files like v2.2_V3_194424_194712/output_853.root from this output, but doing ./adder | grep Error | grep .root doesn't work.

Why is that?


回答1:


Like piokuc's suggests, combine stderr with stdout. However, I think you are looking a better invocation of grep:

./adder 2>&1 | grep "^Error" | grep -oP '[^ ]*\.root'



回答2:


You need to redirect standard error stream (numeric code: 2) from first command in the pipe to standard output stream (numeric code: 1), like this:

$ ./adder 2>&1 | grep Error


来源:https://stackoverflow.com/questions/12055674/piping-error-messages-with-grep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!