On 30/09/15 12:45, Stephen Shirley wrote:
> Hi,
> Here's the scenario: you're in a directory of updating log files
> (could be /var/log), and you want to watch all files for specific
> keywords. For a single file, "tail -F file | grep keyword" is
> sufficient, but if you want to watch multiple files, "tail -F file1
> file2 file3 | grep keyword" is much less helpful because you have no
> way of knowing which log file the matching text is from.
> 
> My suggestion is to add a -H flag (convention taken from grep -H aka
> --with-filename) to tail. With -H specified, tail would no longer
> print out headers before file contents, it would instead prefix the
> line with the file name. With this, "tail -HF file1 file2 file3 | grep
> keyword" is useful, because you get the filename included in the
> matching lines.
> 
> The workaround i've come up with in the meantime is:
> 
>   tail -F "$@" | awk '/^$/ {next} /^==>/ {prefix=$2; next} {print
> prefix ": " $0}'
> 
> but it's a bit of a hack; there's no way to be sure that a header
> string is actually a header, and not part of the file contents.

I like that. It would be similar to the grep option: -H, --with-filename

You could do it with something like:

  $ tail -f .bashrc .vimrc | grep --line-buffered -e'^==> .* <==$' -e 'gco'
  ==> .bashrc <==
  alias gco='git checkout'
  ==> .vimrc <==

However that would impact counting, and show redundant headers.

thanks for the suggestion.
Pádraig


Reply via email to