使用 bash 脚本从日志文件中选择/查看特定数据

使用 bash 脚本从日志文件中选择/查看特定数据

我的 NAS 创建了以下格式的日志文件:

<30>1 2014-07-21T05:02:10+02:00 ABC-NAS qlogd 6432 - - qlogd[6432]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC-PC] finished successfully

<30>1 2014-07-21T05:02:27+02:00 ABC-NAS qlogd 6432 - - qlogd[6432]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC-HTPC] finished successfully

<28>1 2014-07-21T05:10:59+02:00 ABC-NAS qlogd 6920 - - qlogd[6920]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC-NAS] is abnormal shutdown

<30>1 2014-07-21T06:00:15+02:00 ABC-NAS qlogd 6920 - - qlogd[6920]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC Metadata] started

<30>1 2014-07-21T06:00:27+02:00 ABC-NAS qlogd 6920 - - qlogd[6920]: event log: Users: Glacier, Source IP: 127.0.0.1, Computer name: localhost, Content: [Glacier] Backup job [Backup ABC Metadata] finished successfully

使用 BASH 脚本我想创建一个仅包含以下内容的文本文件:

Monday 21 July 2014 - 04:10 AM
Glacier Backup job [Backup ABC-PC] finished successfully

Monday 21 July 2014 - 07:02 AM
Glacier Backup job [Backup ABC-HTPC] finished successfully

等等。

我一直在尝试使用 cat 和 grep,但很快就变得太复杂了。谁能帮我找到正确的方向?

答案1

这是一个非常简单的暴力破解脚本

cat log | cut -d " " -f 2,20- | while read -r date message; do
  echo $date
  echo $message
done

示例输出

2014-07-21T05:02:10+02:00
[Glacier] Backup job [Backup ABC-PC] finished successfully


2014-07-21T05:02:27+02:00
[Glacier] Backup job [Backup ABC-HTPC] finished successfully

如果您希望获得更多功能,那么我强烈建议您学习 Python。

相关内容