Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now