Grep time command output
time
writes its output to stderr, so you need to pipe stderr instead of stdout. But it's also important to remember that time
is 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 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
:
{ time ls -l >/dev/null; } |& grep real
Alternatively, you can use the time
utility, which allows control of the output format. On my system, that utility is found in /usr/bin/time
:
/usr/bin/time -f%e ls -l >/dev/null
man time
for more details on the time
utility.
How to grep the file with time range
awk '$0>="2018-07-11 10" && $0<="2018-07-11 11" && /Connection Refused/' file
Output:
2018-07-11 10:15:06,288 - User logged in Connection Refused
2018-07-11 10:18:06,288 - User logged in Connection Refused
grep the time duration from ping output
Use the PCRE mode on GNU grep
by enabling the -P
flag on, to extract the milli-seconds values alone
ping -4 www.google.com | grep -oP ".*time=\K\d+"
where the \K
escape sequence stands for
\K: This sequence resets the starting point of the reported match. Any previously matched characters are not included in the final matched sequence.
Or you could do away with any GNU-ism tools needed and just use a POSIX sed
to do
ping -4 www.google.com | sed -n 's/.*time=\([[:digit:]]*\).*/\1/p'
Related Topics
Diff to Output Only the File Names
Learning Kernel Hacking and Embedded Development at Home
How to Measure Separate CPU Core Usage for a Process
Are Message Queues Obsolete in Linux
How to Convert an Ssl Certificate in Linux
In Linux Determine If a .A Library/Archive 32-Bit or 64-Bit
Append Line to /Etc/Hosts File with Shell Script
How to Install Packages in Linux (Centos) Without Root User with Automatic Dependency Handling
The Gnu Screen Is Unresponsive, Seems Blocked
How to Perform a 'Git Pull' Without Re-Entering My Ssh Password
JSON Command Line Formatter Tool for Linux
How to Clean Caches Used by the Linux Kernel
Move Window Between Tmux Clients
Lowering Linux Kernel Timer Frequency
Find and Replace in Shell Scripting
What's the Reason Docker Ubuntu Official Image Would Exit Immediately When You Run/Start It