如何使用 shell 和 awk 在日志文件中搜索时间值?
我有一个脚本,用户在其中提供日期、开始时间和结束时间。该脚本将获取这些值并扫描日志文件以搜索日志文件中属于该时间范围的关键字。当用户使用 AM 时间(例如:0700 到 0900 - 这是上午 7 点到 9 点)作为范围或 PM 时间(例如:1400 到 1600 - 这是下午 2 点到 4 点)作为范围时,脚本可以正常工作。当他们使用 AM 开始时间(从 0 - 0700 开始)和结束时间(不从 0 - 1000 开始(这是上午 10 点)时,脚本无法正常运行。
这是我所讨论的行有问题,
echo "Number of ${shipmentTags[$i]} shipments processed in \
log file log$logDate/$logFileName is:
`cat $logDirectory/log$logDate/$logFileName |
awk -F":" '$1$2 >= '"$startTime"' && $1$2 <= '"$endTime"' {print $0}' |
grep ${shipmentStrings[$i]} |
wc -l`"
谢谢。我在这里复制了一些日志的虚拟数据,
07:00:01.124 dfsdfjsdflkjsdfkljsdflkjEDIRequestServiceModule/Routedasdfkl
07:05:02.123 fsldjfsdfskdfjsdfsdkfjEDIRequestServiceModule/Rouedsdfjsdfj
07:10:33.233 sdkjfasdflkjasdfaskdfjasdfkljEDIRequestServiceModule/Routed
09:30:02.222 sefsklfjsdfljksdfEDIRequestServiceModule/Routedasdfdf
14:00:12.222 sdfsdfsdfsdfsdfsdfEDIRequestServiceModule/Routed sdfsdfs
14:01:22.223 sdfsdfsdfsdfsdfsdfsdfEDIRequestServiceModule/Routed sdfsdfsdf
17:00:22.222 sdfsdfsdfsdfsdfsdfEDIRequestServiceModule/Routedsdfsdfsdf
18:00:33.333 sdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdf
如果有人想查看整个脚本,只需回复,我也可以将其剪切并粘贴到此处。
I have a script where a user provides a date, start time and end time. The script will take these values and scan a log file to search for key words in the log file that fall within the time range. When a user uses AM times (Example: 0700 to 0900 - this is 7am to 9am) as a range or PM times (Example: 1400 to 1600 - this is 2pm to 4pm) as a range the script works fine. when they use an AM start time (starting with a zero - 0700) and an end time (doesn't start with zero - 1000 (this is 10am) the script does not function properly.
Here is the line in question that I'm having problems with
echo "Number of ${shipmentTags[$i]} shipments processed in \
log file log$logDate/$logFileName is:
`cat $logDirectory/log$logDate/$logFileName |
awk -F":" '$1$2 >= '"$startTime"' && $1$2 <= '"$endTime"' {print $0}' |
grep ${shipmentStrings[$i]} |
wc -l`"
Thanks. I've copied here just some dummy data of what the log would look like.
07:00:01.124 dfsdfjsdflkjsdfkljsdflkjEDIRequestServiceModule/Routedasdfkl
07:05:02.123 fsldjfsdfskdfjsdfsdkfjEDIRequestServiceModule/Rouedsdfjsdfj
07:10:33.233 sdkjfasdflkjasdfaskdfjasdfkljEDIRequestServiceModule/Routed
09:30:02.222 sefsklfjsdfljksdfEDIRequestServiceModule/Routedasdfdf
14:00:12.222 sdfsdfsdfsdfsdfsdfEDIRequestServiceModule/Routed sdfsdfs
14:01:22.223 sdfsdfsdfsdfsdfsdfsdfEDIRequestServiceModule/Routed sdfsdfsdf
17:00:22.222 sdfsdfsdfsdfsdfsdfEDIRequestServiceModule/Routedsdfsdfsdf
18:00:33.333 sdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdf
If anyone wants to see the whole script, just reply and i can cut and paste it into here as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 AWK 的变量传递。您不必进行所有尴尬的引用。
不要使用
cat
。您可以执行以下操作:或者
另外,AWK 可以为您执行 grep 和计数:
另外,您可能应该使用变量并缩短
echo
。并使用$()
而不是反引号:或者甚至让 AWK 脚本中的
END
子句打印消息:You should use AWK's variable passing. You won't have to do all that awkward quoting.
Don't use
cat
. You can either do:or
Also, AWK can do the grepping and counting for you:
Also, you should probably use a variable and shorten your
echo
. And use$()
instead of backticks:Or even let the
END
clause in the AWK script print the message: