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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Write to file using tail -f through a pipe to grep
# 1  
Old 02-16-2011
Write to file using tail -f through a pipe to grep

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:

Expand|Select|Wrap|Line Numbers
  1. tail -f /var/error_log | grep searchtext > output.txt


The above command fails even though the following command::
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. tail -f /var/error_log > output.txt //without grep
  2. 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?

thanks for the help!!
# 2  
Old 02-16-2011
You just have to wait for the buffer to be filed ...,
or use --line-buffered, if your grep implementation supports this (or a similar) option.

The stdout is buffered, when passed through a pipe.
# 3  
Old 02-17-2011
man 1 unbuffer
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Piping through grep/awk prevents file write

So, this is weird... I'm running this command: iotop -o -P -k -bt -d 5 I'd like to save the output relelvant to rsyslogd to a file, so I do this: iotop -o -P -k -bt -d 5 | grep rsyslogd >> /var/log/rsyslogd Nothing is written to the file! I can write the full output to the file: ... (2 Replies)
Discussion started by: treesloth
2 Replies

2. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Pipe binary file matches grep results to file

I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the following on each file: $ strings foo1 | grep -A1 -B1... (0 Replies)
Discussion started by: chipperuga
0 Replies

4. UNIX for Advanced & Expert Users

write failed: Broken pipe

The "write failed: Broken pipe" message is reported by the file sending PC which run my writed network device driver while 500MB or 900MB is sended! What does the message mean? Does this mean there is a bug in my driver? li,kunlun (11 Replies)
Discussion started by: liklstar
11 Replies

5. Shell Programming and Scripting

Write in a file with pipe also in same line

hi, i want to write in a file the output of one command and pile also the same output like ls -lrt > some_file | wc -l (9 Replies)
Discussion started by: narang.mohit
9 Replies

6. Shell Programming and Scripting

grep a pattern and replace a value in it and write to the same file.

I have some complication with this, I have a file like below for DEV_1 till DEV_10. and the db values are set accordinly which are not unique. For example DEV1,DEV4,DEV6 can have the same target DB name. I waned to identify for DEV_2 and then replace the TARGET_DATABASE value with the new DB... (6 Replies)
Discussion started by: yesmani
6 Replies

7. Shell Programming and Scripting

tail | grep

The program that is running on my machine generates log files. I want to be able to know the number of lines that contain "FT" in the most recent log file. I wrote the following, but it always returns zero. And I know the count is not zero. Any ideas? ls -rt *.log | tail -n 1 | grep -c FT (6 Replies)
Discussion started by: sdilucca
6 Replies

8. UNIX Desktop Questions & Answers

Wall, Write, select users, pipe a text file, HELP Before I'm Bald!

OK... I'm fairly new to unix having the admin handed to me on a platter w/almost no training. However, being a programmer, I do pick up things fairly easily, but this one is getting the best of me. I have a unix server that runs multiple versions of the same ERP system, hand crafted for our... (1 Reply)
Discussion started by: chimodel
1 Replies

9. Shell Programming and Scripting

Grep for NULL in a pipe delimited file

hi, How can I check for a field in a pipe-delimited file having a NULL value in Unix using a grep command or any other command. Please reply (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

10. Shell Programming and Scripting

Pipe Data From Grep Into A File

I am somewhat of a novice at unix scripting and I need a little help. Here is what im trying to do: I am trying to figure out how to pipe the following grep results into a file. /source/programs: grep "WSBL020" W* WBMB991.cbl: COPY WSBL020. WDCB003.cbl: COPY... (4 Replies)
Discussion started by: katinicsdad
4 Replies
Login or Register to Ask a Question