如何使用 shell 和 awk 在日志文件中搜索时间值?

发布于 2024-09-08 19:20:30 字数 1238 浏览 1 评论 0原文

我有一个脚本,用户在其中提供日期、开始时间和结束时间。该脚本将获取这些值并扫描日志文件以搜索日志文件中属于该时间范围的关键字。当用户使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个ˇ局外人 2024-09-15 19:20:30

您应该使用 AWK 的变量传递。您不必进行所有尴尬的引用。

awk -F":" -v start=$startTIME -v end=$endTime '$1$2 >= start && $1$2 <= end {print $0}'

不要使用cat。您可以执行以下操作:

awk ... filename

或者

< filename awk ... 

另外,AWK 可以为您执行 grep 和计数:

< $logDirectory/log$logDate/$logFileName awk -F":" -v start=$startTIME -v end=$endTime -v selection=${shipmentStrings[$i]} '$0 ~ selection { if ($1$2 >= start && $1$2 <= end) count++} END {print count}'

另外,您可能应该使用变量并缩短 echo。并使用 $() 而不是反引号:

shipmentcount=$(... awk ...)
echo "Number of ... is: $shipmentcount"

或者甚至让 AWK 脚本中的 END 子句打印消息:

< $logDirectory/log$logDate/$logFileName \
awk ... -v file=$logDirectory/log$logDate/$logFileName \
    '... END {print "Number of "selection" shipments ... "file" ... is: " count}'

You should use AWK's variable passing. You won't have to do all that awkward quoting.

awk -F":" -v start=$startTIME -v end=$endTime '$1$2 >= start && $1$2 <= end {print $0}'

Don't use cat. You can either do:

awk ... filename

or

< filename awk ... 

Also, AWK can do the grepping and counting for you:

< $logDirectory/log$logDate/$logFileName awk -F":" -v start=$startTIME -v end=$endTime -v selection=${shipmentStrings[$i]} '$0 ~ selection { if ($1$2 >= start && $1$2 <= end) count++} END {print count}'

Also, you should probably use a variable and shorten your echo. And use $() instead of backticks:

shipmentcount=$(... awk ...)
echo "Number of ... is: $shipmentcount"

Or even let the END clause in the AWK script print the message:

< $logDirectory/log$logDate/$logFileName \
awk ... -v file=$logDirectory/log$logDate/$logFileName \
    '... END {print "Number of "selection" shipments ... "file" ... is: " count}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文