Linux Grep 时间命令输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17257724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:17:14  来源:igfitidea点击:

Grep time command output

linux

提问by jettisamba

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 greponly the realvalue 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

采纳答案by rici

timewrites its output to stderr, so you need to pipe stderr instead of stdout. But it's also important to remember that timeis part of the syntax of bash, and it times an entire pipeline. Consequently, you need to wrap the pipeline in braces, or run it in a subshell:

time将其输出写入 stderr,因此您需要通过管道传输 stderr 而不是 stdout。但同样重要的是要记住这time是 bash 语法的一部分,它对整个管道进行计时。因此,您需要将管道包装在大括号中,或在子 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 stdoutand stderr:

使用 Bash v4.0(可能在 Linux 发行版上通用,但在 Mac OS X 上仍然不是标准),您可以使用|&管道stdoutstderr

{ time ls -l >/dev/null; } |& grep real

Alternatively, you can use the timeutility, which allows control of the output format. On my system, that utility is found in /usr/bin/time:

或者,您可以使用该time实用程序,它允许控制输出格式。在我的系统上,该实用程序位于/usr/bin/time

/usr/bin/time -f%e ls -l >/dev/null 

man timefor more details on the timeutility.

man time有关该time实用程序的更多详细信息。

回答by Clyde

(time ls -l)  2>&1 > /dev/null |grep real

This redirects stderr (which is where time sends its output) to the same stream as stdout, then redirects stdout to dev/null so the output of ls is not captured, then pipes what is now the output of time into the stdin of grep.

这会将 stderr(时间发送其输出的地方)重定向到与 stdout 相同的流,然后将 stdout 重定向到 dev/null 以便不捕获 ls 的输出,然后将现在的时间输出通过管道传输到 grep 的 stdin 中。

回答by doubleDown

If you just want to specify the output format of timebuiltin, you can modify the value of TIMEFORMATenvironment variable instead of filtering it with grep.

如果只想指定timebuiltin的输出格式,可以修改TIMEFORMAT环境变量的值,而不是用grep.

In you case,

在你的情况下,

TIMEFORMAT=%R
time ls -l

would give you the "real" time only.

只会给你“真实”的时间。

Here's the linkto relevant information in Bash manual (under "TIMEFORMAT").

这是Bash 手册中相关信息的链接(在“TIMEFORMAT”下)。

Thisis a similar question on SO about parsing the output of time.

是关于解析time.

回答by VeLKerr

I think, it can be made a little easier:

我认为,它可以变得更容易一些:

time ls &> /dev/null | grep real

回答by Immotus

Look out.. bash has a built-in "time" command. Here are some of the differences..

注意.. 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