On 01/10/15 17:07, Pádraig Brady wrote:
> 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
Upon more careful consideration, I'm 50:50
about adding per line processing to tail.
More robust awk would be:
tail -Fv "$@" | awk '
/^==> .* <==$/ {prefix=substr($0,5,length-8); next}
{print prefix ":" $0}
' |
grep 'blah'
Now whether that's AWKward enough and not general enough
to warrant a new tail option I'm not sure.
Perhaps we could just add the above snippet to the docs?
The big advantage is that it works everywhere already.
cheers,
Pádraig.