Shell – tail -f, insert line break after log is idle for 3 seconds

shell-scripttailtext processing

When doing a tail -f error.log, how to insert programmatically a line break after nothing has been appened to the file for 3 seconds ?

(obviously, once one line break has been added, no other line break should be added until other line(s) of text is added to the log file)

For instance, these lines are appened to error.log :

foo
bar
boo [[wait 4 seconds]]
2far
2foo
2bar
2boo [[wait 40 seconds]]
2far

This would be the output in the console :

foo
bar
boo

2far
2foo
2bar
2boo

2far

Best Answer

You could always implement the tail -f (well here, unless you uncomment the seek(), more like tail -n +1 -f as we're dumping the whole file) by hand with perl for instance:

perl -e '
  $| = 1;
  # seek STDIN, 0, 2; # uncomment if you want to skip the text that is
                      # already there. Or if using the ksh93 shell, add
                      # a <((EOF)) after < your-file
  while (1) {
    if ($_ = <STDIN>) {
      print; $t = 0
    } else {
      print "\n"            if $t == 3;
      # and a line of "-"s after 10 seconds:
      print "-" x 72 . "\n" if $t == 10;
      sleep 1;
      $t++;
    }
  }' < your-file

Or let tail -f do the tailing and use perl to insert the newlines if there's no input for 3 seconds:

tail -f file | perl -pe 'BEGIN{$SIG{ALRM} = sub {print "\n"}} alarm 3'

Those assume that the output itself is not slowed down (like when the output goes to a pipe that is not actively read).

Related Question