awk - 使用 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'
解决方案
第一个错误
在搜索和表达式中,我们有一些名为特殊字符的东西,例如/
, *
, ... 要在正常模式下使用它们,您需要\
在开头添加:\/
, \*
, \...
.
总之,这段代码是错误的:
$ 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
推荐阅读
- regex - 正则表达式 (Regex):如何在多行 html 标记中查找属性缺失
- flutter - 每次调用 build 方法时会发生什么?是否创建了一个新的状态对象?如果是这样,那么它应该每次调用 initstate 方法吗?
- provisioning-profile - Firebase 应用分发 - 为新的 UDID 更新现有 IPA
- python - 基于环境的 env vars 的不同值
- c - 为什么尚未到达文件末尾时会收到 EOF?
- javascript - 动态创建哈希哈希?
- django - 考虑到多对多关系的一个元素,如何对模型列表进行排序?
- php - (失败)net::ERR_CONNECTION_RESET 上大文件上传 php
- javascript - 如何在 Jest 的 xyz.test.js 中导入“.mjs”模块?
- java - JUNITS:MockRestServiceServer 未使用 Maven 构建运行