命令行:监视日志文件并将数据添加到数据库 [英] Command line: monitor log file and add data to database
本文介绍了命令行:监视日志文件并将数据添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在监视日志文件。每行的格式如下:
I'm monitoring a log file. Each line has the following format:
2012 5 29 14 20 438.815 872.737 -1.89976 -0.55156 8.68749 -0.497848 -0.54559 0 0 6 00 0 0 0 0 0 0 0 0 80 9 0 0 10 0 0 0 8 00 9 0 0 0 0 0 0 2 41 84 0 0 0 1 0
正如你所看到的,每个值由一个制表符分隔。
As you can see, each value is delimited by a tab.
如何写一个Perl脚本每一个新的数据行(日志文件每十分钟更新一次),并将这些数据插入MySQL数据库?
How can I write a Perl script to take each new line of data (the log file is updated every ten minutes) and insert this data into a MySQL database?
我想这么做
如果我执行 tail -f -n 1 ./Eamorr.out>
If I do tail -f -n 1 ./Eamorr.out > myPerlScript.pl
, will my perl script get data each time the file is appended to?
提前非常感谢,
推荐答案
bash中的另一个aproach:
Another aproach in bash :
#!/usr/bin/perl -w
use strict;
$|++; # unbuffer output
open FH, "tail -f /var/log/syslog |";
while (<FH>) { chomp; print; }
在纯shell中没有Perl:
Without Perl in pure shell :
tail -f /var/log/syslog |
while read a; do
echo "INSERT INTO FOOBAR VALUES($(
sed "s/ /','/g; s/^/'/; s/$/'/" <<< "$a")
);"
done
这篇关于命令行:监视日志文件并将数据添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文