Reputation: 534
I've made an script which measure the time of some processes. This is the file that I get:
real 0m6.768s
real 0m5.719s
real 0m5.173s
real 0m4.245s
real 0m5.257s
real 0m5.479s
real 0m6.446s
real 0m5.418s
real 0m5.654s
The command I use to get the time is this one:
{ time my-command } |& grep real >> times.txt
What I need is to sum all this times and get as a result how many (hours if applies) minutes and seconds using a bash script.
Upvotes: 2
Views: 463
Reputation: 6742
Pipe the output to this command
grep real | awk '{ gsub("m","*60+",$2); gsub("s","+",$2); printf("%s",$2); } END { printf("0\n"); }' | bc
This should work if you have generated the output using built-in time command. The output is in seconds.
Upvotes: 0
Reputation: 19315
From man bash
, then if PAGER is less / time
.
If the time reserved word precedes a pipeline, the elapsed as well as user and system time consumed by its exe- cution are reported when the pipeline terminates. The -p option changes the output format to that specified by POSIX. The TIMEFORMAT variable may be set to a format string that specifies how the timing information should be displayed; see the description of TIMEFORMAT under Shell Variables below.
then /TIMEFORMAT
The optional l specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the fraction is included. If this variable is not set, bash acts as if it had the value $'\nreal\t%3lR\nuser\t%3lU\nsys%3lS'. If the value is null, no timing information is displayed. A trailing newline is added when the format string is displayed.
If it can be changed to something like
TIMEFORMAT=$'\nreal\t%3R'
without the l
, it may be easier to sum.
Note also format may depend on locale LANG
:
compare
(LANG=fr_FR.UTF-8; time sleep 1)
and
(LANG=C; time sleep 1)
In that case the sum can be done with an external tool like awk
awk '/^real/ {sum+=$2} END{print sum} ' times.txt
or perl
perl -aln -e '$sum+=$F[1] if /^real/; END{print $sum}' times.txt
Upvotes: 1