我正在使用 cPanel帐户并拥有 Apache 2.4访问日志,可以存储其日志,如:
66.249.93.30 - - [04/May/2018:21:26:39 +0200] "GET / HTTP/1.1" 302 207 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko; Google Page Speed Insights) Chrome/41.0.2272.118 Safari/537.36"
66.249.93.30 - - [05/May/2018:10:26:39 +0200] "GET / HTTP/1.1" 302 207 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko; Google Page Speed Insights) Chrome/41.0.2272.118 Safari/537.36"
日期格式为日期“+%d /%B /%Y:%k:%M:%S”
使用bash脚本我想只提取在过去一小时内记录的行,例如:
完整日志文件:
66.249.93.30 - - [04/May/2018:21:26:39 +0200] First Line
66.249.93.30 - - [05/May/2018:11:00:21 +0200] Second Line
66.249.93.30 - - [05/May/2018:11:15:39 +0200] Third Line
66.249.93.30 - - [05/May/2018:12:00:11 +0200] Fourth Line
当前时间: 05 / May / 2018: 12:01 :06
日志: 5月5日 11:01 - 12:01
的时间间隔过滤后的输出:
66.249.93.30 - - [05/May/2018:11:15:39 +0200] Third Line
66.249.93.30 - - [05/May/2018:12:00:11 +0200] Fourth Line
我尝试过使用awk和其他一些建议,但我无法让它工作,任何帮助将不胜感激!
答案 0 :(得分:0)
$ date
Sat, May 05, 2018 10:49:13 AM
$ cat tst.awk
{
split($4,t,/[[ :\/]/)
mthNr = sprintf("%02d",(index("JanFebMarAprMayJunJulAugSepOctNovDec",t[3])+2)/3)
curTime = t[4] mthNr t[2] t[5] t[6] t[7]
}
curTime >= minTime
$ awk -v minTime=$(date -d '60 min ago' '+%Y%m%d%H%M%S') -f tst.awk file
66.249.93.30 - - [05/May/2018:11:00:21 +0200] Second Line
66.249.93.30 - - [05/May/2018:11:15:39 +0200] Third Line
66.249.93.30 - - [05/May/2018:12:00:11 +0200] Fourth Line
使用问题中的时间来获得问题中的预期输出:
$ awk -v minTime=$(date -d '2018/05/05 11:01:06' '+%Y%m%d%H%M%S') -f tst.awk file
66.249.93.30 - - [05/May/2018:11:15:39 +0200] Third Line
66.249.93.30 - - [05/May/2018:12:00:11 +0200] Fourth Line
答案 1 :(得分:-1)
我能够弄明白!
我必须将 04 / May / 2018:21:26:39 转换为UNIX时间戳。这是通过以下日期
的使用来完成的public partial class Login : Form
{
szofttech2Entities context = new szofttech2Entities();
public Login()
{
InitializeComponent();
}
private void buttonEnter_Click(object sender, EventArgs e)
{
var password = string.Empty;
try
{
password = (from u in context.Users
where u.UserName == textBoxUserName.Text
select u.Password).Single();
}
catch (InvalidOperationException)
{
MessageBox.Show($"None or more than one user found matching {textBoxUserName.Text}");
}
if (!string.IsNullOrEmpty(password))
{
if (textBoxPassword.Text == password)
{
Order order = new Order();
order.Show();
this.Hide();
}
else
{
labelWrongUserPassword.Visible = true;
}
}
}
}
然后制作另一个 UNIX时间戳,落后60分钟
date -d "YEAR-MONTH-DAY HR:M:S" "+%S"
在条件过滤器中,UNIX时间戳较大(-gt)的所有日志条目比时间戳晚60分钟
使用我当前的设置:
cPanel + Apache 2.4
记录格式: / home / $ USER / public_html_cron_logs / $ DAY / $ HOUR- $ MINUTE- [GET | POST] .log
喜欢/home/$USER/public_html_cron_logs/05-05-2018/14-53-GET.log
date -d "60 min ago" "+%s"