Discussion:
[Help-bash] howto redirect time output to perl -pe "chomp;"?
Larry Evans
2017-01-03 16:07:21 UTC
Permalink
I've read and reread:

https://www.gnu.org/software/bash/manual/bash.html#Appending-Standard-Output-and-Standard-Error
https://www.gnu.org/software/bash/manual/bash.html#Pipelines
and tried every permuation I could think of:

--{--cut here--
***@lje-OptiPlex-9020:~$ echo $SHELL
/bin/bash
***@lje-OptiPlex-9020:~$ time 2>time.out

real 0m0.000s
user 0m0.000s
sys 0m0.000s
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-9020:~$ time > time.out

real 0m0.000s
user 0m0.000s
sys 0m0.000s
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-9020:~$ time 2> time.out

real 0m0.000s
user 0m0.000s
sys 0m0.000s
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-9020:~$ time |perl -pe "chomp"
bash: syntax error near unexpected token `|'
***@lje-OptiPlex-9020:~$ time sleep 1|perl -pe "chomp"

real 0m1.003s
user 0m0.004s
sys 0m0.000s
***@lje-OptiPlex-9020:~$ time sleep 1|&perl -pe "chomp">time.out

real 0m1.003s
user 0m0.000s
sys 0m0.004s
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-9020:~$ time sleep 1|& perl -pe "chomp">time.out

real 0m1.003s
user 0m0.004s
sys 0m0.000s
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-9020:~$ echo "hello" | perl -pe "chomp">time.out
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-9020:~$ echo "hello;" | perl -pe "chomp">time.out
***@lje-OptiPlex-9020:~$ cat time.out
hello;***@lje-OptiPlex-9020:~$ echo $SHELL
/bin/bash
***@lje-OptiPlex-9020:~$ echo "hello" | perl -pe "chomp">time.out
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-902time sleep 2|& perl -pe "chomp">time.out

real 0m2.003s
user 0m0.004s
sys 0m0.000s
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-9020:~$ TIMEFORMAT='%3R';time sleep 1 &>| perl -pe
"chomp"
bash: syntax error near unexpected token `|'
***@lje-OptiPlex-9020:~$ TIMEFORMAT='%3R';time sleep 1 >| perl -pe
"chomp"
sleep: invalid option -- 'p'
Try 'sleep --help' for more information.
0.001
***@lje-OptiPlex-9020:~$ TIMEFORMAT='%3R';time sleep 1 | perl -pe "chomp"
1.001
***@lje-OptiPlex-9020:~$ TIMEFORMAT='%3R';time sleep 1 | perl -pe
"chomp" > time.out
1.003
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-9020:~$ TIMEFORMAT='%3R';time sleep 1 |& perl -pe
"chomp" > time.out
1.002
***@lje-OptiPlex-9020:~$ cat time.out
***@lje-OptiPlex-9020:~$

--}--cut here--

Please, how do I get the output from time wittout the newline?

-regards,
Larry
Greg Wooledge
2017-01-03 16:21:57 UTC
Permalink
How can I redirect the output of 'time' to a variable or file?

http://mywiki.wooledge.org/BashFAQ/032
Larry Evans
2017-01-03 16:47:43 UTC
Permalink
Post by Greg Wooledge
How can I redirect the output of 'time' to a variable or file?
http://mywiki.wooledge.org/BashFAQ/032
Thanks very much to both you and Chet. It worked perfectly.
Larry Evans
2017-01-03 20:09:58 UTC
Permalink
Post by Greg Wooledge
How can I redirect the output of 'time' to a variable or file?
http://mywiki.wooledge.org/BashFAQ/032
Thanks again; however, now I can't get tabs in the TIMEFORMAT.
Searching in:

https://www.gnu.org/software/bash/manual/bash.html#Bash-Variables

for TIMEFORMAT lead to the following:

If this variable is not set, Bash acts as if it had the value

$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'

So, I figured I could use:

TIMEFORMAT='\nreal\t%1R'

However, when I did, with the following gmake target:

SHELL:=/bin/bash
#^to enable use of TIMEFORMAT below.
.ONESHELL:
STRIP_NL:=perl -pe "chomp;"

time:
echo -n "$@{">time.out
TIMEFORMAT='\nreal\t%1R'
{ time sleep .22222 ; } 2>&1 | $(STRIP_NL) >>time.out
echo "}$@">>time.out
show:time
@echo "+++$@:"
@cat time.out
@echo "---$@:"

I got this:

make -f time.mk show
echo -n "time{">time.out
TIMEFORMAT='\nreal\t%1R'
{ time sleep .22222 ; } 2>&1 | perl -pe "chomp;" >>time.out
echo "}time">>time.out
+++show:
time{\nreal\t0.2}time
---show:

Compilation finished at Tue Jan 3 14:03:13

How do I get tabs into the output?

TIA.

-regards,
Larry
Chet Ramey
2017-01-03 21:14:50 UTC
Permalink
Post by Larry Evans
If this variable is not set, Bash acts as if it had the value
$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
TIMEFORMAT='\nreal\t%1R'
You forgot the leading `$'.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
Larry Evans
2017-01-03 21:23:53 UTC
Permalink
Post by Chet Ramey
Post by Larry Evans
If this variable is not set, Bash acts as if it had the value
$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
TIMEFORMAT='\nreal\t%1R'
You forgot the leading `$'.
I should have also said I tried that, but got this:

make -f time.mk show
echo -n "time{">time.out
TIMEFORMAT=\nreal\t%1R'
{ time sleep .22222 ; } 2>&1 | perl -pe "chomp;" >>time.out
echo "}time">>time.out
/bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 4: syntax error: unexpected end of file
time.mk:24: recipe for target 'time' failed
make: *** [time] Error 1

Compilation exited abnormally with code 2 at Tue Jan 3 15:22:10

I've attached the complete makefile and hope that helps.
Larry Evans
2017-01-03 21:56:10 UTC
Permalink
Post by Larry Evans
Post by Chet Ramey
Post by Larry Evans
If this variable is not set, Bash acts as if it had the value
$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
TIMEFORMAT='\nreal\t%1R'
You forgot the leading `$'.
make -f time.mk show
echo -n "time{">time.out
TIMEFORMAT=\nreal\t%1R'
{ time sleep .22222 ; } 2>&1 | perl -pe "chomp;" >>time.out
echo "}time">>time.out
/bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 4: syntax error: unexpected end of file
time.mk:24: recipe for target 'time' failed
make: *** [time] Error 1
Compilation exited abnormally with code 2 at Tue Jan 3 15:22:10
I've attached the complete makefile and hope that helps.
OOPS. I should have used $$ instead of just $ inside the makefile.
With that change, it works.
Sorry for noise :(
Chet Ramey
2017-01-04 13:27:57 UTC
Permalink
Post by Chet Ramey
Post by Larry Evans
If this variable is not set, Bash acts as if it had the value
$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
TIMEFORMAT='\nreal\t%1R'
You forgot the leading `$'.
You need to double the `$' to get it through make and into the shell
that runs the recipe.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
Chet Ramey
2017-01-03 16:31:20 UTC
Permalink
Post by Larry Evans
https://www.gnu.org/software/bash/manual/bash.html#Appending-Standard-Output-and-Standard-Error
https://www.gnu.org/software/bash/manual/bash.html#Pipelines
The key is that `time' is not a builtin command; it's a reserved word that
modifies an attribute of a command or pipeline. The timing information
is printed by the shell after it executes the command or pipeline to
be timed. So you need to run a timed command in a context where you
can capture the output of that context, like a subshell or group command.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
Loading...