Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error when calling SSH command 20 106
Find non-UTF Filenames 1 50
How to eliminate duplicates from a list of hosts in groups 2 49
linux pull logs and export FTP 4 58
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

20 Experts available now in Live!

Get 1:1 Help Now