T
T
Toast2016-04-11 04:46:36
linux
Toast, 2016-04-11 04:46:36

How to get write value to variable from changing output of Linux command?

I need to get one value from the output.

tail -f access.log | pv -l -a -i10 >/dev/null 
[4.2/s]

The problem is that the command is running in streaming mode. Is it possible to get a single value from the output of such commands?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Selivanov, 2016-04-11
@selivanov_pavel

Suspiciously crooked and uncomfortable task. I suspect that you need to do something differently, I suggest writing an original problem here and discussing how to solve it.
If you really want perversions, you can send the output of the first command with `tee` to a fifo file, and from there read the desired line in parallel with something like `sed -n '5p' | cut -d\ -f2`
My colleague got interested in the question and came up with the following solution:

tailf access.log | while read -r line ; do
((a++)) ;
nd=$(date "+%s");  [ -z ${ld+x} ]  && ld=$nd || if (( $nd-$ld >=10 )); then 
echo  "$a/10"|bc ; ld=$nd; a=0;
fi;
done

Looking at which, it becomes obvious that you still don’t need to write such a bash :)
The second option from a colleague:
tailf access.log | awk 'BEGIN { st=systime(); } {  ++count ;  ct=systime(); if (ct-st >= 10) { print count/10 ; count=0; st=ct } }'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question