日志档案:
2021-08-29 23:25:46,398 IST INFO :......mailslot_create: creating socket for querying route
2021-08-29 23:25:46,398 IST INFO :.....mailbox_register: no mailbox necessary for forward
2021-08-29 23:25:46,398 IST INFO :......mailslot_create: creating mailslot for route engine - informational socket
2021-08-30 22:00:00,017 IST INFO :.....mailslot_create: creating mailslot for RSVP
2021-08-30 22:00:00,017 IST INFO :....mailbox_register: mailbox allocated for rsvp
2021-08-30 22:00:00,017 IST INFO :.....mailslot_create: creating mailslot for RSVP via UDP
2021-08-31 12:10:41,469 IST INFO :.....mailslot_create: creating mailslot for RSVP
2021-08-31 12:10:41,469 IST INFO :....mailbox_register: mailbox allocated for rsvp
2021-08-31 12:10:41,469 IST INFO :.....mailslot_create: creating mailslot for RSVP via UDP
2021-08-31 12:10:41,469 ISTINFO :....mailbox_register: mailbox allocated for rsvp-udp
2021-08-31 12:10:41,469 IST TRACE :..entity_initialize: interface 127.0.0.1, entity for rsvp allocated and initialized
我想要打印如下输出:
First occurrence of date: 2021-08-29
Last occurrence of date:2021-08-31
答案1
您需要一个可用于日期的正则表达式,例如,使用grep
简单范围来[0-9]
匹配 0-9 之间的一位数字值
grep '20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]' log_file
grep --help
提供一些标志
-o
仅过滤匹配项(日期)-m
限制最大结果
这将只给你第一行。顺便说一下,这里grep
应用于 stdout 而不是文件
cat log_file | grep -om1 '20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]'
这将只给你最后一行。tac
这里是从下往上输出的。
tac log_file | grep -om1 '20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]'