[英]Grep time command output
使用time ls
,我有以下输出:
$ time ls -l
total 2
-rwx------+ 1 FRIENDS None 97 Jun 23 08:59 location.txt
-rw-r--r--+ 1 FRIENDS None 10 Jun 23 09:06 welcome
real 0m0.040s
user 0m0.000s
sys 0m0.031s
现在,当我尝试仅grep
实际值行时,实际结果是:
$ time ls -l | grep real
real 0m0.040s
user 0m0.000s
sys 0m0.031s
我的问题是,如何只获得真正的价值作为输出? 在这种情况下, 0m0.040s
。
time
将其输出写入stderr,因此您需要管道stderr而不是stdout。 但同样重要的是要记住, time
是bash语法的一部分,它会占用整个流水线。 因此,您需要将管道包装在大括号中,或者在子shell中运行它:
$ { time ls -l >/dev/null; } 2>&1 | grep real
real 0m0.005s
使用Bash v4.0(在Linux发行版上可能是通用的,但在Mac OS X上仍然不是标准版),您可以使用|&
管道stdout
和stderr
:
{ time ls -l >/dev/null; } |& grep real
或者,您可以使用time
实用程序,它允许控制输出格式。 在我的系统上,该实用程序位于/usr/bin/time
:
/usr/bin/time -f%e ls -l >/dev/null
man time
有关详细信息, time
效用。
(time ls -l) 2>&1 > /dev/null |grep real
这会将stderr(时间发送其输出的地方)重定向到与stdout相同的流,然后将stdout重定向到dev / null,这样就不会捕获ls的输出,然后将现在输出的时间输入到grep的stdin中。
请注意.. bash有一个内置的“时间”命令。 以下是一些差异..
# GNU time command (can also use $TIMEFORMAT variable instead of -f)
bash> /usr/bin/time -f%e ls >/dev/null
0.00
# BASH built-in time command (can also use $TIME variable instead of -f)
bash> time -f%e ls >/dev/null
-f%e: command not found
real 0m0.005s
user 0m0.004s
sys 0m0.000s
我想,它可以变得更容易一些:
time ls &> /dev/null | grep real
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.