使用 head -n 从测试结果中检索前几行

使用 head -n 从测试结果中检索前几行

我正在测试一个 Python 程序并希望检索其前两行结果

$ python twoSum.py  
DEBUG-MainProcess:31921-MainThread:140190102726464 find: 151 
DEBUG-MainProcess:31921-MainThread:140190102726464 j: None
DEBUG-MainProcess:31921-MainThread:140190102726464 find: 194 
DEBUG-MainProcess:31921-MainThread:140190102726464 j: None
DEBUG-MainProcess:31921-MainThread:140190102726464 find: 247 
DEBUG-MainProcess:31921-MainThread:140190102726464 j: None
DEBUG-MainProcess:31921-MainThread:140190102726464 find: 192 
DEBUG-MainProcess:31921-MainThread:140190102726464 j: None
DEBUG-MainProcess:31921-MainThread:140190102726464 find: 206 
DEBUG-MainProcess:31921-MainThread:140190102726464 j: None
DEBUG-MainProcess:31921-MainThread:140190102726464 find: 171 
DEBUG-MainProcess:31921-MainThread:140190102726464 j: None
DEBUG-MainProcess:31921-MainThread:140190102726464 find: 170 
DEBUG-MainProcess:31921-MainThread:140190102726464 j: None
DEBUG-MainProcess:31921-MainThread:140190102726464 find: 190 
DEBUG-MainProcess:31921-MainThread:140190102726464 j: None
DEBUG-MainProcess:31921-MainThread:140190102726464 find: 132 
DEBUG-MainProcess:31921-MainThread:140190102726464 j: 63
[8, 63]
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

处理head文本

$ python twoSum.py  | head -n 1
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 259 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: None
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 235 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: None
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 133 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: None
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 274 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: None
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 265 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: None
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 257 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: None
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 280 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: None
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 134 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: None
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 138 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: None
DEBUG-MainProcess:31978-MainThread:140699277834048 find: 145 
DEBUG-MainProcess:31978-MainThread:140699277834048 j: 75
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

它不起作用,
我怎样才能检索我想要看到的前几行。

答案1

输出很可能DEBUG-是流向标准错误流而不是标准输出流 - 尝试

python twoSum.py 2>&1 | head -n 1 

stderr(文件描述符 #2)重定向到stdout(文件描述符 #1);或者(bash 简写)

python twoSum.py |& head -n 1

欲了解更多信息,请参阅相关问题使用带有管道和 & 符号的 grep 来过滤 find 中的错误

相关内容