[英]Grep time command output
Using time ls
, I have the following 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
Now, when I try to grep
only the real value line, the actual result is: 现在,当我尝试仅
grep
实际值行时,实际结果是:
$ time ls -l | grep real
real 0m0.040s
user 0m0.000s
sys 0m0.031s
My question is, how to get only the real value as output? 我的问题是,如何只获得真正的价值作为输出? In this case,
0m0.040s
. 在这种情况下,
0m0.040s
。
time
writes its output to stderr, so you need to pipe stderr instead of stdout. time
将其输出写入stderr,因此您需要管道stderr而不是stdout。 But it's also important to remember that time
is part of the syntax of bash, and it times an entire pipeline. 但同样重要的是要记住,
time
是bash语法的一部分,它会占用整个流水线。 Consequently, you need to wrap the pipeline in braces, or run it in a subshell: 因此,您需要将管道包装在大括号中,或者在子shell中运行它:
$ { time ls -l >/dev/null; } 2>&1 | grep real
real 0m0.005s
With Bash v4.0 (probably universal on Linux distributions but still not standard on Mac OS X), you can use |&
to pipe both stdout
and stderr
: 使用Bash v4.0(在Linux发行版上可能是通用的,但在Mac OS X上仍然不是标准版),您可以使用
|&
管道stdout
和stderr
:
{ time ls -l >/dev/null; } |& grep real
Alternatively, you can use the time
utility, which allows control of the output format. 或者,您可以使用
time
实用程序,它允许控制输出格式。 On my system, that utility is found in /usr/bin/time
: 在我的系统上,该实用程序位于
/usr/bin/time
:
/usr/bin/time -f%e ls -l >/dev/null
man time
for more details on the time
utility. man time
有关详细信息, time
效用。
(time ls -l) 2>&1 > /dev/null |grep real
这会将stderr(时间发送其输出的地方)重定向到与stdout相同的流,然后将stdout重定向到dev / null,这样就不会捕获ls的输出,然后将现在输出的时间输入到grep的stdin中。
If you just want to specify the output format of time
builtin , you can modify the value of TIMEFORMAT
environment variable instead of filtering it with grep
. 如果您只想指定
time
builtin的输出格式,可以修改TIMEFORMAT
环境变量的值,而不是使用grep
过滤它。
In you case, 在你的情况下,
TIMEFORMAT=%R
time ls -l
would give you the "real" time only. 会给你“真正的”时间。
Here's the link to relevant information in Bash manual (under "TIMEFORMAT"). 以下是 Bash手册中相关信息的链接 (在“TIMEFORMAT”下)。
This is a similar question on SO about parsing the output of time
. 关于解析
time
输出, 这是一个类似的问题。
Look out.. bash has a built-in "time" command. 请注意.. bash有一个内置的“时间”命令。 Here are some of the differences..
以下是一些差异..
# 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.