Created
August 20, 2018 09:54
-
-
Save chenjiancan/20d6761b611edc55356678cc41f8eaa0 to your computer and use it in GitHub Desktop.
监控指定日志文件,要求从在指定时间之后的日志开始扫描,日志中出现 ‘installed memtester’ 行,打印出来
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# awk 支持直接比较时间字符串,并通过 ~ match表达式匹配包含字符串 | |
awk '$1>="2018-08-17"&&$2>"09:55:20"&&$0 ~ /installed memtester/ {print $0}' /var/log/dpkg.log | |
# 输出是: 2018-08-17 09:55:30 status installed memtester:amd64 4.3.0-3 | |
# 如果需要持续监控日志文件,可以使用 tail -1f 持续的输出最后一行,并通过 | pipe 交给awk 处理, exit 表示在匹配到一次后退出 | |
tail -f /var/log/dpkg.log | awk '$1>="2018-08-17"&&$2>"09:55:20"&&$0 ~ /installed memtester/ {print $0; exit}' | |
# 可通过 tail -Nf (N 为行数)来指定从倒数第几行开始扫描 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2018-08-11 22:48:37 status installed man-db:amd64 2.7.5-1 | |
2018-08-11 22:48:37 startup packages configure | |
2018-08-11 22:48:37 configure tree:amd64 1.7.0-3 <none> | |
2018-08-11 22:48:37 status unpacked tree:amd64 1.7.0-3 | |
2018-08-11 22:48:37 status half-configured tree:amd64 1.7.0-3 | |
2018-08-11 22:48:37 status installed tree:amd64 1.7.0-3 | |
2018-08-11 22:48:38 startup packages configure | |
2018-08-16 23:08:58 startup archives unpack | |
2018-08-16 23:09:03 install htop:amd64 <none> 2.0.1-1ubuntu1 | |
2018-08-16 23:09:03 status half-installed htop:amd64 2.0.1-1ubuntu1 | |
2018-08-16 23:09:03 status triggers-pending mime-support:all 3.59ubuntu1 | |
2018-08-16 23:09:04 status triggers-pending man-db:amd64 2.7.5-1 | |
2018-08-16 23:09:04 status unpacked htop:amd64 2.0.1-1ubuntu1 | |
2018-08-16 23:09:04 status unpacked htop:amd64 2.0.1-1ubuntu1 | |
2018-08-16 23:09:05 trigproc mime-support:all 3.59ubuntu1 <none> | |
2018-08-16 23:09:05 status half-configured mime-support:all 3.59ubuntu1 | |
2018-08-16 23:09:05 status installed mime-support:all 3.59ubuntu1 | |
2018-08-16 23:09:05 trigproc man-db:amd64 2.7.5-1 <none> | |
2018-08-16 23:09:05 status half-configured man-db:amd64 2.7.5-1 | |
2018-08-16 23:09:18 status installed man-db:amd64 2.7.5-1 | |
2018-08-16 23:09:19 startup packages configure | |
2018-08-16 23:09:19 configure htop:amd64 2.0.1-1ubuntu1 <none> | |
2018-08-16 23:09:19 status unpacked htop:amd64 2.0.1-1ubuntu1 | |
2018-08-16 23:09:19 status half-configured htop:amd64 2.0.1-1ubuntu1 | |
2018-08-16 23:09:19 status installed htop:amd64 2.0.1-1ubuntu1 | |
2018-08-16 23:09:19 startup packages configure | |
2018-08-17 09:55:11 startup archives unpack | |
2018-08-17 09:55:14 install memtester:amd64 <none> 4.3.0-3 | |
2018-08-17 09:55:14 status half-installed memtester:amd64 4.3.0-3 | |
2018-08-17 09:55:15 status triggers-pending man-db:amd64 2.7.5-1 | |
2018-08-17 09:55:15 status unpacked memtester:amd64 4.3.0-3 | |
2018-08-17 09:55:15 status unpacked memtester:amd64 4.3.0-3 | |
2018-08-17 09:55:16 trigproc man-db:amd64 2.7.5-1 <none> | |
2018-08-17 09:55:16 status half-configured man-db:amd64 2.7.5-1 | |
2018-08-17 09:55:29 status installed man-db:amd64 2.7.5-1 | |
2018-08-17 09:55:29 startup packages configure | |
2018-08-17 09:55:29 configure memtester:amd64 4.3.0-3 <none> | |
2018-08-17 09:55:29 status unpacked memtester:amd64 4.3.0-3 | |
2018-08-17 09:55:29 status half-configured memtester:amd64 4.3.0-3 | |
2018-08-17 09:55:30 status installed memtester:amd64 4.3.0-3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment