【发布时间】:2018-06-22 21:29:39
【问题描述】:
我想编写一个脚本,询问用户我们想要过滤日志数据的时间间隔的第一个和最后一个日期和时间,我需要一些帮助。
我不知道如何真正找到该范围内的数据,因为我不能使用单个正则表达式。
我的日志文件如下所示:
108.162.221.147 - - [04/Aug/2016:18:59:59 +0200] "GET / HTTP/1.1" 200 10254 "-"...
141.101.99.235 - - [04/Aug/2016:19:00:00 +0200] "GET / HTTP/1.1" 200 10255 ...
108.162.242.219 - - [04/Aug/2016:19:00:00 +0200] "GET / HTTP/1.1" 200 10255...
185.63.252.237 - - [04/Aug/2016:19:00:00 +0200] "CONNECT...
108.162.221.147 - - [04/Aug/2016:19:00:00 +0200] "GET /?...
185.63.252.237 - - [04/Aug/2016:19:00:01 +0200] "CONNECT....
etc...
我的脚本:
#!/bin/bash
echo "enter the log file name "
read fname
echo "enter the start date and time "
read startdate
echo "enter the end fate and time "
read enddate
result=$(some code for filtering rows from this range)
echo "$result" > 'log_results'
echo "results written into /root/log_results file"
我尝试过使用
sed -n "/"$startdate"/,/"$enddate"/p" "fname"
由于斜杠而看不到日期格式,所以没有说,正则表达式也不起作用,因为它只能从日志中找到这两个日期(也许我写错了)
我该怎么做?
【问题讨论】:
标签: bash shell centos sh logfile