user3341592
user3341592

Reputation: 1561

Auto-insert blank lines in `tail -f`

Having a log file such as:

[DEBUG][2016-06-24 11:10:10,064][DataSourceImpl] - [line A...]
[DEBUG][2016-06-24 11:10:10,069][DataSourceImpl] - [line B...]
[DEBUG][2016-06-24 11:10:12,112][DataSourceImpl] - [line C...]

which is under tail -f real-time monitoring, is it possible to auto-insert (via a command we would pipe to the tail) "blank lines" after, let's say, 2 seconds of inactivity?

Expected result:

[DEBUG][2016-06-24 11:10:10,064][DataSourceImpl] - [line A...]
[DEBUG][2016-06-24 11:10:10,069][DataSourceImpl] - [line B...]
---
[DEBUG][2016-06-24 11:10:12,112][DataSourceImpl] - [line C...]

(because there is a gap of more than 2 seconds between 2 successive lines).

Upvotes: 1

Views: 634

Answers (2)

Andreas Louv
Andreas Louv

Reputation: 47099

awk -F'[][\\- ,:]+' '1'

The above will split fields on ], [, -, ,, and :, so that each field is as described below:

[DEBUG][2016-06-24 11:10:10,064][DataSourceImpl] - [line A...]
 22222  3333 44 55 66 77 88 999  ...

You can then concatenate some of the fields and use that to measure time difference:

tail -f input.log | awk -F'[][\\- ,:]+' '{ curr=$3$4$5$6$7$8$9 }
                      prev + 2000 < curr { print "" } # Print empty line if two seconds 
                                                      # have passed since last record.
                                         { prev=curr } 1'

Upvotes: 3

Chris Maes
Chris Maes

Reputation: 37762

tail does not have such feature. If you want you could implement a program or script that checks the last line of the file; something like (pseudocode)

previous_last_line = last line of your file
while(sleep 2 seconds)
    {
    if (last_line == previous_last_line)
        print newline
    else
        print lines since previous_last_line
    }

Two remarks:

  • this will cause you to have no output during 2 seconds; you could keep checking for the last line more often and keep a timestamp; but that requires more code...
  • this depends on the fact that all lines are unique; which is reasonable in your case; since you have timestamps in each line

Upvotes: 0

Related Questions