I'm lazy and I could write a script to do this, but I'm even too lazy to think of how to do it.
I often do things like :
cris$ python runexperiment.py > output.txt
cris$ cat output.txt
Sometimes when looking at the long output of an experiment I like to let the page just scroll and watch the successive patterns form and disperse. But using cat on a file with 1 million lines finishes in maybe 5 seconds. This is too quick even for me.
Is there any way that I can slow down the speed of viewing the file, something like a 'scroll utility'? I want fast, but not 200k lines a second (all of which presumably the display would never even register anyway).
Something like
cris$ scroll -lps=300 output.txt
And then sitting back and watching 300 lines per second roll past would be ideal, I imagine.
Best Answer
Short and readable:
I post this solutions because they are small and readable, as comments of DMas's answer seem promote this kind of solution!
But I hate this because: For this run, perl will fork to
/bin/sleep
300x / seconds!This is a big ressource consumer! Also a wrong good solutions!!
Using builtin sleep in perl
Unfortunely, builtin
sleep
is limited to integers. So we have to useselect
instead:Under perl,
print while <>
could be replaced by the-p
switch:Let's try:
Explanation:
300 lines / sec means 1 line by 0.0033333333 secs.
print
without argument prints$_
which is default input space.called as
... | perl -e
,... | perl -ne
or... | perl -pe
, standard input would be automaticaly assigned to*STDIN
which is default file descriptor, so<>
would do the same as<STDIN>
which will read from standard input until$/
(input record separator which is by default a newline) will be reached. In English, by default<>
will read one line from standard input and assign content to$_
variable.&&
is an and condition, but is used there as a chain command separator so after (successfully) print one line, doing next command.select
is a programmer's trick to not usesleep
. This command is designed to trap events on file descriptors (inputs and/or outputs, files, socket and/or net sockets). With this command, a program could wait for 3 kind of events, feed ready to read, feed ready to write and some event happened on feed. The fourth argument is a timeout in seconds, so syntax isselect <feeds where wait for input>, <feeds where having to write>, <feed where something could happen>, <timeout>
.For more precision, you could use
Time::Hires
perl module:Note:
$.
is current input line number.Better written as
cat >catLps.pl
Usage:
So we could:
For fun: