【发布时间】:2015-04-06 14:32:26
【问题描述】:
我正在尝试提取两个给定日期的日志。如果我指定像这样 Apr 02 15:21:28 这样的日期,代码工作正常,我的意思是如果知道准确的分钟和秒的时间,但是代码会因为我传递这样的值而失败 从 Apr 02 15* 到 Apr 04 15* 这里 15 是小时,实际上我想制作用户只需要添加日期和时间的脚本(仅以小时为单位,没有分钟或秒)
> #!/bin/bash
read -p " enter the App name : " app
file="/logs/$app/$app.log"
read -p " Enter the Date in this Format --'10 Jan 20 or Jan 10 20' : " first
read -p " Enter the End time of logs : " end
if [ -f "$file" ]
then
if grep -q "$first" "$file"; then
final_first=$first
fi
if grep -q "$end" "$file"; then
final_end=$end
fi
sed -n " /$final_first/,/$final_end/ "p $file >$app.txt
else
echo "$app.log not found, Please check correct log name in deployer"
fi
样本数据:
Apr 07 12:39:15 DEBUG [http-0.0.0.0-8089-21] model.DSSAuthorizationModel - pathInfo : /about-ses
Apr 07 12:39:15 DEBUG [http-0.0.0.0-8089-21] servlet.CasperServlet - Request about to be serviced by model: com.ge.oilandgas.sts.model.SessionValidModel
【问题讨论】:
-
尝试将值作为
Apr 02 15传递,不带星号。请注意,这只有在日志文件中有匹配条目时才有效——如果 4 月 2 日 15:00 到 16:00 之间没有发生任何事情,则范围将永远不会匹配。 -
@Wintermute 是的,你是对的,我刚刚通过了 15 年 4 月 2 日和 15 年 4 月 4 日,这里 sed 找到了第一个日期,但没有找到 15 年 4 月 4 日,因此他提取了所有行,直到日志文件结束,即 Apr 06 ...你能告诉任何可以很好替代这个的代码吗,我的意思是如果没有找到 15 年 4 月 4 日,至少他可以在 4 月 4 日停止,不会超过 4 月 5 日
-
您需要一个知道
01 May 03晚于30 Apr 22的工具,而sed 并不那么聪明。我会使用 Perl,但您可能更喜欢 shell 脚本。 -
从日志文件中包含一些示例行,以便我们查看时间戳的确切格式。
-
@glennjackman 我更新了我的代码,如果有的话,我想在这做什么.. 如果条件失败,它应该将空值放入 sed final first 和 final end 变量,并且 sed 命令将不起作用这个案例。
标签: linux shell variables if-statement scripting