无法在日期范围之间进行 grep 模式
Posted
技术标签:
【中文标题】无法在日期范围之间进行 grep 模式【英文标题】:Not able to grep pattern between a date range 【发布时间】:2022-01-08 17:45:31 【问题描述】:我正在尝试获取指定日期范围内的行。我尝试了许多在线解决方案,但它们都不起作用
下面是我的日志文件
Nov 21 03:31:28 Sample Log test
Nov 21 03:32:01 Sample Log test
Nov 21 03:33:01 Sample Log test
Nov 21 03:34:01 Sample Log test
Nov 21 03:35:02 Sample Log test
Nov 21 03:36:01 Sample Log test
Nov 21 03:37:01 Sample Log test
Nov 21 03:38:01 Sample Log test
Nov 21 03:39:01 Sample Log test
Nov 21 03:39:01 Sample Log test
Nov 21 03:39:01 Sample Log test
Nov 21 03:40:01 Sample Log test
Nov 21 03:40:01 Sample Log test
Nov 21 03:40:29 Sample Log test
Nov 21 03:40:29 Sample Log test
Nov 21 03:41:01 Sample Log test
Nov 21 03:41:22 Sample Log test
Nov 21 03:41:22 Sample Log test
Nov 21 03:41:43 Sample Log test
Nov 21 03:41:43 Sample Log test
Nov 21 03:42:01 Sample Log test
我正在使用的 awk 命令是
-bash-4.2$ b="03:31:28"
-bash-4.2$ e="03:41:00"
-bash-4.2$ awk -v "b=$b" -v "e=$e" -F ',' '$1 >= b && $1 <= e' ~/test
-bash-4.2$
它没有返回输出
【问题讨论】:
样本数据中是否只有一天的日志条目? 为什么在文件中没有逗号的情况下使用逗号作为字段分隔符 (-F ','
)?为什么要比较第 1 个字段 ($1
) 而您的时间字段是第 3 个字段?
【参考方案1】:
使用awk
$ awk -v b=03:31:28 -v e=03:41:00 '$3 >= b && $3 <= e' input_file
Nov 21 03:31:28 Sample Log test
Nov 21 03:32:01 Sample Log test
Nov 21 03:33:01 Sample Log test
Nov 21 03:34:01 Sample Log test
Nov 21 03:35:02 Sample Log test
Nov 21 03:36:01 Sample Log test
Nov 21 03:37:01 Sample Log test
Nov 21 03:38:01 Sample Log test
Nov 21 03:39:01 Sample Log test
Nov 21 03:39:01 Sample Log test
Nov 21 03:39:01 Sample Log test
Nov 21 03:40:01 Sample Log test
Nov 21 03:40:01 Sample Log test
Nov 21 03:40:29 Sample Log test
Nov 21 03:40:29 Sample Log test
【讨论】:
以上是关于无法在日期范围之间进行 grep 模式的主要内容,如果未能解决你的问题,请参考以下文章