Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write to a file after piping output from tail -f through to grep

I'm looking to write to a file after piping output from tail -f through to grep. Say,write to a file "temp" for all lines with "Playing:" within in error_log "FreeSwitch.log".

 tail -f "/var/lof/freeswitch/freeswitch.log" | grep "Playing:" > temp

but not working ! It is a centos 5.5

like image 486
Reddy Avatar asked Mar 17 '11 15:03

Reddy


People also ask

How do you grep a pipe result?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".

How do you grep on tail?

To grep the output of tail simply tell the grep command to red from stdin and pipe the output from tail to grep as follows.


2 Answers

Maybe you have an issue with buffering? See BashFAQ: What is buffering?

You could e.g. try:

tail -f /var/lof/freeswitch/freeswitch.log | grep --line-buffered "Playing:" > temp
like image 67
bmk Avatar answered Sep 28 '22 07:09

bmk


-f, --follow[={name|descriptor}]
              output appended data as the file grows;

It scans the file as it grows. And it is a process with an interval. You can only interrupt it.

Use parameter:

-c, --bytes=K
              output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file  

or

-n, --lines=K
              output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth

EDIT: as bmk said:

grep --line-buffered  

think it will help you

like image 37
Sergey Avatar answered Sep 28 '22 08:09

Sergey