我正在监控一个日志文件。每行具有以下格式:
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
如您所见,每个值都由制表符分隔。
如何编写 Perl 脚本来获取每一行新数据(日志文件每十分钟更新一次)并将这些数据插入 MySQL 数据库?
我想尽可能多地在命令行上完成这些工作。
如果我执行 tail -f -n 1 ./Eamorr.out > myPerlScript.pl
,我的 perl 脚本会在每次附加文件时获取数据吗?
非常感谢,
最佳答案
bash 中的另一种方法:
#!/usr/bin/perl -w
use strict;
$|++; # unbuffer output
open FH, "tail -f /var/log/syslog |";
while (<FH>) { chomp; print; }
在纯 shell 中没有 Perl:
tail -f /var/log/syslog |
while read a; do
echo "INSERT INTO FOOBAR VALUES($(
sed "s/ /','/g; s/^/'/; s/$/'/" <<< "$a")
);"
done
关于linux - 命令行 : monitor log file and add data to database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10801034/