过滤 time(1) 输出以仅获取时间值
如何在 OS X 下过滤某些时间命令输出值? 我已经尝试过
➜ ~ (time ls -la)| grep system|awk "{print $4, $6, $8, $10}"
ls -G -la 0.01s user 0.01s system 16% cpu 0.102 total
,但我只想取数字,在本例中为“0.01 0.01 16 0.102” 谢谢
编辑:Paul指出,输出可能转到stderr所以我的解决方案是将输出“重定向”到标准输出并继续破解字符串:)
(time ls -la) 2>&1 | grep system | tr -dc '[:digit:]. '
how do I filter certain time command output values under OS X?
I have tried
➜ ~ (time ls -la)| grep system|awk "{print $4, $6, $8, $10}"
ls -G -la 0.01s user 0.01s system 16% cpu 0.102 total
however I want to take only numbers, in this case "0.01 0.01 16 0.102"
Thanks
Edit: Paul pointed out, that output might go to stderr so my solution is to "redirect" output to stdout and continue hacking strings :)
(time ls -la) 2>&1 | grep system | tr -dc '[:digit:]. '
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 zsh 内置
time
时,设置变量TIMEFMT
来控制输出(请参阅man zshparam
)。要在不带字母字符串的情况下重现默认输出:
例如,星号会阻止
s
的输出“秒”。当然,如果您真正感兴趣的话,您可以将其限制为一个特定字段。
重定向 time 的输出以将其捕获在变量中或进一步处理它:
我将命令的输出重定向到
/ dev/null
,但如果你想捕获它,只需删除该重定向即可。When using the zsh builtin
time
, set the variableTIMEFMT
to control the output (seeman zshparam
).To reproduce the default output without the alpha strings:
The asterisks prevent the output of
s
for "seconds", for example.You can, of course, limit it to one particular field if that's all you're really interested in.
Redirect the output of time to capture it in a variable or process it further:
I redirected the output of the command to
/dev/null
, but if you want to capture it just remove that redirection.这里的通用答案是,为了处理
time
的输出,您必须有一个类似于Which times the command in a subshell 并隐藏其输出的结构,然后捕获 time 的输出(即在 stderr(又名描述符 2)上并将其重定向到 stdout,然后将 stdout 定向到管道。之后可以进行进一步的处理。
正如 Dennis Williamson 指出的,zsh 的 TIMEFMT 可用于自定义输出。这很好,因为这意味着您在使用 zsh 时可能不必构建管道来重新格式化 time 的输出,但您仍然需要执行 stderr 重定向来捕获输出。
The generic answer here is that in order to process the output of
time
in general you must have a construct likeWhich times the command in a subshell and hides its output, then captures the output of time (which is on stderr, aka descriptor 2) and redirects it to stdout, then directs stdout to a pipe. After that further processing is possible.
As Dennis Williamson noted, zsh's TIMEFMT can be used to customize the output. This is nice because it means you probably don't have to construct a pipeline to reformat time's output when using zsh, but you still have to do the stderr redirection to capture the output.
有点黑客,但你可以删除非数字字符吗?
编辑:此外,输出可能会发送到 stderr,因此使用 2>&1 将其重定向到 stdin
Bit of a hack, but you could just remove non-numeric characters?
EDIT: Also, the output may be going to stderr so redirect it to stdin with 2>&1