Solved

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

Posted on 2007-03-26
5
94 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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