Linux – redirecting “tail –follow=name | grep” to a file

greplinuxredirect

Here is my scenario:

I have a log file that is being wrote too by an application. The log file is named "alarms.log". I want to tail the file for a certain string and redirect that to a file called alarms.log.test. So i am using the below command.

tail --follow=name alarms.log | grep CEC >> alarms.log.test

Then i tail the file it is being redirected to by using this command:

tail --follow=name alarms.log.test

My problem is this: The file alarms.log.test is not getting anything in the file. I know for a fact that it should be. Because the string is being found. I believe the problem is that the redirect waits for a certain amount of data is read and then flushes to the redirect file, is that the case?

I need it to immediately flush to the file every time something is found. Is there a way to do this?

Thanks

Best Answer

Can you use the tee command for this?

The tee command is used to store and view (both at the same time) the output of any other command.

Maybe something like the example linked on SuperUser

tail -f alarms.log | egrep --line-buffered 'name' | tee alarms.log.test

Also see: http://linux.101hacks.com/unix/tee-command-examples/