sed 命令没有为所使用的正则表达式提供正确的输出

sed 命令没有为所使用的正则表达式提供正确的输出

我正在尝试从日志文件中获取一系列时间间隔之间的数据。但是,不会获取结束日期的完整数据。

下面是使用的命令:

 sed -n '/26/Nov/2018/,/30/Nov/2018/ p' /path/file.log 

我在上面的日志文件中得到的数据有点像:

 192.168.0.123 - - [25/Nov/2018:13:24:59 +0530] "Data 1"
 192.168.0.123 - - [26/Nov/2018:13:24:59 +0530] "Data 2"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 3"
 192.168.0.123 - - [27/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [27/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [28/Nov/2018:13:25:53 +0530] "Data 7"
 192.168.0.123 - - [29/Nov/2018:13:25:53 +0530] "Data 8"
 192.168.0.123 - - [30/Nov/2018:13:25:53 +0530] "Data 9"
 192.168.0.123 - - [30/Nov/2018:13:25:53 +0530] "Data 10"
 192.168.0.123 - - [01/Dec/2018:13:25:53 +0530] "Data 11"
 192.168.0.123 - - [01/Dec/2018:13:25:53 +0530] "Data 12"

答案1

这是因为您既使用了/sed match 命令的分隔符,又使用了匹配字符串的一部分。你需要逃离内部/。试试这个:

$  sed -n '/26\/Nov\/2018/,/30\/Nov\/2018/ p' file.log 
 192.168.0.123 - - [26/Nov/2018:13:24:59 +0530] "Data 2"
 192.168.0.123 - - [26/Nov/2018:13:25:09 +0530] "Data 3"
 192.168.0.123 - - [27/Nov/2018:13:25:09 +0530] "Data 4"
 192.168.0.123 - - [27/Nov/2018:13:25:11 +0530] "Data 5"
 192.168.0.123 - - [27/Nov/2018:13:25:53 +0530] "Data 6"
 192.168.0.123 - - [28/Nov/2018:13:25:53 +0530] "Data 7"
 192.168.0.123 - - [29/Nov/2018:13:25:53 +0530] "Data 8"
 192.168.0.123 - - [30/Nov/2018:13:25:53 +0530] "Data 9"

相关内容