Assuming you can restart the program and put it in a pipe, you can then pipe it to xargs
, with the following caveats:
- you want to run the command one line at a time (-n 1)
- you want to put the output inside a parameter (-i)
- you probably want the message to run as soon as the
tail
reports it, so you need tail
to be line buffered.
Depending on what you have installed:
stdbuf -o 0 tail -f FILE | xargs -i -n 1 terminal-notifier --message "{}"
unbuffer tail -f FILE | xargs " "
and I saw a tailf
version that supported the -l
(line-buffered) option.
You can also emulate tail -f
in Perl.
And you can replace xargs
(even if it's pretty standard and ubiquitous) with a specially crafted for
loop in the shell (it's not as simple as it looks, though; it's similar to processing filenames with spaces in a loop).
If you cannot restart the program, you need to intercept its stdout. Here's a Stack Overflow answer about that. Other utilities and scripts such as reredirect are described here; I don't know about their compatibility with MacOS, though.
Further modifications
The output can be parsed normally with utilities such as sed or cut:
unbuffer tail -f FILE | grep animal | sed -e 's/animal (.*) gear/\1/' | xargs ...
This way we would have:
Output by tail output by grep output by sed
correct horse battery staple
animal rhino gear animal rhino gear rhino
llama is animal llama is animal llama is animal
llama peruvian
animal ant gearbox animal ant gearbox antbox
(--line-buffered
option to grep
may be used)
terminal-notifier
for each new line thattail -f
reports, have a look atxargs