Solved

Write to file using tail -f through a pipe to grep

Posted on 2007-03-26
5
96 Views
Last Modified: 2015-08-26
Hi -- I'm looking to write to a file after piping output from tail -f through to grep:

#write to a file for all lines with "searchtext" within in error_log:
tail -f /var/error_log | grep searchtext > output.txt

The above command fails even though the following command::

tail -f /var/error_log | grep searchtext

does produce output, as expected.  Nothing is getting through while redirecting to files after a tail -f...  The following work:

tail -f /var/error_log > output.txt                //without grep
tail /var/error_log | grep searchtext > output.txt   //without -f

So it's just the combination of grep and tail with specifically the -f (following) option that causes the problem.  It would be ideal to be able to do this with a single inline command.  Any ideas?
0
Comment
Question by:minnirok
5 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 18796222
You could do

tail -f /var/error_log | grep searchtext | tee >output.txt
0
 
LVL 84

Expert Comment

by:ozo
ID: 18796341
the problem may be that the pipe is buffering the output
0
 
LVL 20

Accepted Solution

by:
edster9999 earned 75 total points
ID: 22785074
Grep is buffering the content and not delivering it until it has filled the buffer or got to end of file (which it will not do)

tail -f /var/error_log | grep  --line-buffered searchtext > output.txt

This may not work on very old copies of grep.


0
 
LVL 8

Assisted Solution

by:devil_himself
devil_himself earned 50 total points
ID: 23679223
bash is not involved in buffering the output of processes it spawns nor can be controlled from the shell.The program itself determines whether or how much buffering is performed or when the output buffer is flushed.

tail -f "file" | grep "text"

tail command buffers its output, and therefore grep only gets its input until there is a certain amount (typically 2k or 4k) or until the buffer is flushed or the tail process terminates.

tail -f "file" | grep "text" > logfile

If output is going to a 'terminal', then the buffer is flushed on every newline character, but this does not happen if it is going to a file. if the output file is a pipe their is no line buffering.

to work around it use a 'while read'  loop
tail -f file | while read line ; do echo "$line" grep 'text' >> logfile ; done & 

or you can use 'unbuffer' utility which disables the output buffering.
http://linux.die.net/man/1/unbuffer
0
 
LVL 7

Author Closing Comment

by:minnirok
ID: 40949049
Apologize for not buffering my response in a timely fashion.  Great answers, guys!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question