I have this command (for pomodoros):
play -n synth 25:00 pinknoise
I don't want to silent completely the output (-q
option), just the header (grep
don't work).
Normal output:
File Size: 94.3T
Encoding: n/a
Channels: 1 @ 32-bit
Samplerate: 48000Hz
Replaygain: off
Duration: unknown
In:0.00% 00:00:01.02 [00:00:00.00] Out:49.2k [======|======] Hd:1.3 Clip:0
Desire filtered output: 01.02
(this number is updated, like in a cURL or pv
progress bar)
How can I grep just that part of the output?
So far:
For some reason the output is sent to stderr, like with "Permission denied" from
find
. An easy way to test is to add at the end2> /dev/null
.Maybe the reason why sox/play output to stderr is because it supports writing the output to standard output (stdout) using the special filename
-
(see sox man page).But
|& grep "^In"
won't work. Using|& tee log.txt
it seems uses the delete character to update the last line.I tried
grep --line-buffered
,unbuffer
andstdbuf
(after reading this and this) with some great progress:play -n synth 25:00 pinknoise 2>&1 | stdbuf -oL tr '\r' '\n' | grep -o '[0-9][0-9]*\.[0-9][0-9] '
That's very close!
Is possible to get only just one updated line like was on the original output? Maybe like this loop with echo -ne
.
I'm not sure why something like | grep --line-buffered .
doesn't work, neither removing trailing newline: | tr -d '\n'
. I need something like tail -f -n 1
.
You can grep the output if you pipe stderr as well, e.g.:
From Pipelines section of GNU Bash manual:
However, this will not work for the progress line: CLI commands usually test whether the output is to a terminal and discard updated output if it isn’t. We need a hacky workaround to get around that. First redirect the full output to a file:
This blocks the current terminal and you can not just send it to the background because then it discards the progress line again. So to get this line, open a second terminal in the same directory and process the file, e.g.:
The advantage of using
tail
is that you also get theAborted.
line when theplay
exited: