Reputation: 990
I need to run many times an aplication and get some statistcs using some commands. A simple exemple of what I'm traying to do is:
/usr/bin/time --format="%U" date
This line will print:
Seg Nov 2 22:15:28 BRST 2015
0.00
I need to format this output, and I want to do this using bash, like this:
echo "$usertime & $date"
where $usertime
is the result from /usr/bin/time --format="%U"
and $date
the result of date
I know that the below works, but I don't know how to separate the values into two variables:
OUTPUT=`/usr/bin/time --format="%U" date`
echo "$OUTPUT"
the output from above is:
0.00
Seg Nov 2 22:23:50 BRST 2015
(in reverse order)
Upvotes: 1
Views: 63
Reputation: 113924
Here, awk is used to reverse the line order and format the output:
$ /usr/bin/time --format="%U" date 2>&1 | awk 'NR==1{d=$0;next} {print $0 " & " d}'
0.00 & Mon Nov 2 16:58:40 PST 2015
$ /usr/bin/time --format="%U" date 2>&1 | sed -E 'N; s/([^\n]*)\n(.*)/\2 \& \1/'
0.00 & Mon Nov 2 17:01:15 PST 2015
$ /usr/bin/time --format="%U" date 2>&1 | ( read d; read t; echo "$t & $d" )
0.00 & Mon Nov 2 17:04:40 PST 2015
Upvotes: 1
Reputation: 653
/usr/bin/time is writing the time to standard error, whereas the output of date is going to standard output. If you want them in separate variables, you can write the output of time to a file with the -o
flag, then read it back in to another variable:
OUTPUT=`/usr/bin/time -o timefile --format="%U" date`
TIME=$(<timefile)
Upvotes: 0