Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stateful tail (only shows the new lines from the last execution)

I want to be able to see how many lines were added to a file since the last quering without reading the whole file again.

Something like :

ptail my_file | fgrep "[ERROR]" | wc -l 

A solution in simple Perl would be prefered, since I don't have an easy access to a compiler.

like image 851
Steve Schnepp Avatar asked Oct 29 '08 20:10

Steve Schnepp


2 Answers

Although it consumed the lines for other purposes, I have written code which does essentially this before.

All you need to do is record the byte offset (with tell) and inode (with stat) for each file after the tail is complete. The next time it's run against the file, first check the inode (with stat) again. If the inode has changed or the file is smaller than the recorded offset, then it's a different file (deleted and recreated, log got rotated, etc.), so you should show it from the beginning; otherwise, seek to the recorded offset and display it from there.

like image 86
Dave Sherohman Avatar answered Nov 15 '22 05:11

Dave Sherohman


since does exactly that although it is in C.

like image 29
Steve Schnepp Avatar answered Nov 15 '22 04:11

Steve Schnepp