首页 > 解决方案 > 使用 sed 或 awk 使用日期搜索循环遍历文本文件

问题描述

您好 sed 和 awk 专家正在尝试在使用日期时间搜索时遍历具有以下内容的日志文件。
我想在2021/08/09 11:18:10和之间循环2021/08/09 10:49:32 日期格式是YYYY/MM/DD

2021/08/09 10:22:24 202108091022G5a Outbound text with ref number 2
2021/08/09 10:31:44 202108091031GhG Outbound text with ref number 3
2021/08/09 10:31:51 202108091031KZL Outbound text with ref number 4
2021/08/09 10:49:32 2021080910496ZT Outbound text with ref number 5
2021/08/09 11:02:27 2021080911025eQ Outbound text with ref number 6
2021/08/09 11:14:28 202108091114Aim Outbound text with ref number 6
2021/08/09 11:15:13 202108091115bRi Outbound text with ref number 7
2021/08/09 11:17:11 202108091117KIK Outbound text with ref number 8
2021/08/09 11:18:10 202108091118dB5 Outbound text with ref number 9
2021/08/09 11:18:17 202108091118qxN Outbound text with ref number 10
2021/08/09 11:19:28 202108091119TuI Outbound text with ref number 11 

我在下面尝试了我的,但它返回错误

sed -n '/2021/08/09 09:00:00/,/2021/08/09 11:00:00/p'  Desktop/smslog.log
sed: -e expression #1, char 7: unknown command: `0'

标签: awksed

解决方案


第一个错误

在搜索和表达式中,我们有一些名为特殊字符的东西,例如/, *, ... 要在正常模式下使用它们,您需要\在开头添加:\/, \*, \....

总之,这段代码是错误的:

$ sed -n '/2021/08/09 09:00:00/,/2021/08/09 11:00:00/p' Desktop/smslog.log

正确的语法:

$ sed -n '\#^2021/08/09 10:#,\#^2021/08/09 11:#p' Desktop/smslog.log
  • 在这段代码中,我们使用了与Ed建议的原始帖子不同的格式,但你明白了。

第二个错误

您的sed -n命令将仅查找包含您的BEGIN/的指定行END,但在此查询中,我们没有任何日志完全适用于2021\/08\/09 09:00:00,因此这不会向您显示任何内容,因为它找不到该日志以开始和结束你的END观点。

例如,您可以这样做来获取每个11:--:--消息11:--:--

$ sed -n '\#^2021/08/09 11:#,\#^2021/08/09 11:#p' Desktop/smslog.log
2021/08/09 11:02:27 2021080911025eQ Outbound text with ref number 6
2021/08/09 11:14:28 202108091114Aim Outbound text with ref number 6
2021/08/09 11:15:13 202108091115bRi Outbound text with ref number 7
2021/08/09 11:17:11 202108091117KIK Outbound text with ref number 8
2021/08/09 11:18:10 202108091118dB5 Outbound text with ref number 9
2021/08/09 11:18:17 202108091118qxN Outbound text with ref number 10
2021/08/09 11:19:28 202108091119TuI Outbound text with ref number 11

$ sed -n '\#^2021/08/09 10:#,\#^2021/08/09 10:#p' Desktop/smslog.log
2021/08/09 10:22:24 202108091022G5a Outbound text with ref number 2
2021/08/09 10:31:44 202108091031GhG Outbound text with ref number 3
2021/08/09 10:31:51 202108091031KZL Outbound text with ref number 4
2021/08/09 10:49:32 2021080910496ZT Outbound text with ref number 5

$ sed -n '\#^2021/08/09 10:#,\#^2021/08/09 11:#p' Desktop/smslog.log
2021/08/09 10:22:24 202108091022G5a Outbound text with ref number 2
2021/08/09 10:31:44 202108091031GhG Outbound text with ref number 3
2021/08/09 10:31:51 202108091031KZL Outbound text with ref number 4
2021/08/09 10:49:32 2021080910496ZT Outbound text with ref number 5
2021/08/09 11:02:27 2021080911025eQ Outbound text with ref number 6
2021/08/09 11:14:28 202108091114Aim Outbound text with ref number 6
2021/08/09 11:15:13 202108091115bRi Outbound text with ref number 7
2021/08/09 11:17:11 202108091117KIK Outbound text with ref number 8
2021/08/09 11:18:10 202108091118dB5 Outbound text with ref number 9
2021/08/09 11:18:17 202108091118qxN Outbound text with ref number 10
2021/08/09 11:19:28 202108091119TuI Outbound text with ref number 11 

推荐阅读