Announcing retail – tail with regular expressions

I have broken down and implemented my own tail command. retail can output that part of a file (or pipe) following the last match of a regular expression, which can be useful for logfiles and various other kinds of data. It is also a fully compliant implementation of the POSIX.1-2008 tail command, so you can theoretically replace your system tail with it.

The driving use case of this for me is log files – I want to get all of the file after the current occurrence of some event. After establishing that I couldn’t do that with sed, and that although I probably could with awk it would be a bad idea, I set about writing this in C. It does exactly what I want, and I’ve also been over the POSIX tail spec to add everything from that as well, so it is a usable tail command.

The code is on GitHub (for the moment at least), accessible with `git clone git://`, or in an automatic tarball . Although it satisfies my original use-case there are a couple of additions I’d still like to make along with efficiency improvements, and, of course – patches welcome.

Some usage examples:

retail -r Beginning logfile.log

Output everything after the last occurrence of “Beginning” in the file.

retail -r Beginning -u 'succeeded|error' -f logfile.log

Same as the last one, and continue reading as any lines are appended until one matches /succeeded|error/ (i.e., it contains either of those words), and then terminate.

retail -n +10

Start printing at line 10, until the end of the file. Just like in regular tail. Negative numbers, bare [-+]N, and -c work too.

Once more, retail.

Leave a Reply

You must be logged in to post a comment.