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

断了今生、忘了曾经 提交于 2019-11-30 17:00:42

问题


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


回答1:


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



回答2:


-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




回答3:


Did you put the file name after the >?

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



回答4:


thanks for your help.

here is my code to insert into mysql with the word "error":

tail -f /var/log/httpd/error_log | \
grep -E --line-buffered "error" | \
while read line; do \
#echo -e "MY LINE: ${line}"; done
echo "INSERT INTO logs (logid,date,log) VALUES (NULL, NOW(), '${line}');" | mysql -uUSERDB -pPASSDB DBNAME; done


来源:https://stackoverflow.com/questions/5341297/write-to-a-file-after-piping-output-from-tail-f-through-to-grep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!