【发布时间】:2015-03-04 23:50:03
【问题描述】:
我已经编写了 awk 来打印以空格分隔的日志文件中的特定行。
if [ ! -f $userlog ]
then
awk -F' ' '$7$8 == "Loginitialized" {print $2" "$3" "$4" "$5" process start"}''$7$8$9 == "Applictionsuccessfullyended" {print $2" "$3" "$4" "$5" process end"}' $preplog > $userlog
fi
输出样本(用户日志)
2015-03-02 13:14:25 (PID 19594) process start
2015-03-02 22:42:29 (PID 30473) process start
2015-03-02 22:53:20 (PID 30473) process end
2015-03-03 07:16:55 (PID 31078) process start
2015-03-03 14:53:15 (PID 16591) process start
2015-03-03 14:54:10 (PID 18292) process start
我需要在 $preplog 上使用相同的 awk,但从最后一行开始,我已经用它打印了。 我正在尝试,但我失败了:
if [ -f $userlog ]
then
lastpid=`awk -F' ' 'END{print $4}' $userlog` #pid ostatniego procesu
cat $preplog | awk 'BEGIN{ found=0} /PID /$lastpid/ Log initialized/{found=1} {if (found) {awk -F" " "$7$8 == "Loginitialized" {print $2" "$3" "$4" "$5" process start"}''$7$8$9 == "Applictionsuccessfullyended" {print $2" "$3" "$4" "$5" process end"}"} ;}' >> $userlog
fi
但我的 awk 编程能力并不强。我真的不知道如何咬它。
【问题讨论】:
-
不是很清楚你想要什么,你能根据你之前的样本添加预期的输出吗?
-
通常的解决方法是使用日志轮换,并在轮换时分析旧的日志文件。