在从tail -f到grep的管道输出之后写入文件

时间:2011-03-17 15:31:45

标签: linux unix grep tail centos5

我希望在从tail -f到grep的管道输出之后写入文件。 比如说,在error_log“FreeSwitch.log”中写入“播放:”所有行的文件“temp”。

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

但没有工作!这是一个5.5美元

4 个答案:

答案 0 :(得分:12)

也许你有缓冲问题?请参阅BashFAQ: What is buffering

你可以,例如尝试:

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

答案 1 :(得分:2)

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

它会随着文件的增长扫描文件。这是一个间隔的过程。你只能打断它。

使用参数:

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

-n, --lines=K
              output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth
编辑:正如bmk所说:

grep --line-buffered  

认为它会帮助你

答案 2 :(得分:1)

您是否将文件名放在>之后?

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

答案 3 :(得分:0)

感谢您的帮助。

这是我的代码,用“错误”一词插入mysql:

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
相关问题