【发布时间】:2015-03-12 15:01:45
【问题描述】:
我必须从任何应用程序日志中提取特定日期的日志。我们通常做什么
- vi app.log
- /日期(搜索)
- .=(找到日期的位置)
- G(最后一行)
- first_line,Last_line w filname.log(生成特定日期的日志文件)
我做了小脚本,但我认为我的逻辑不正确:
#!/bin/bash
read -p " enter the App name" : app
read =p " enter the Date in this Format 10 Jan 20:01:00" : date
FILE=/logs/$app.log
# Check if $app exists or not
if test ! -f "$app" /logs
then
echo "Error - $app not found."
else
First_line=$(cat $FILE | grep $date | .=)
Second_line=$(tail -1 $FILE | .=)
vi $FILE | $First_line, $Second_line w $app.txt
fi
【问题讨论】:
-
1. read =p " 以这种格式输入日期 10 Jan 20:01:00" : date ...... 2 First_line=$(cat $FILE | grep $date | .=)
-
在
sed或类似的情况下使用vim会使工作变得不必要地困难。例如,sed -n '/2015-01-02/,$p' app.log将打印从包含 2015 年 1 月 2 日的第一行到文件末尾的所有行。如果您的日期格式不同,请相应地修改搜索模式。请注意,如果您具体了解时间,您可能永远不会有记录(例如,如果您搜索 20:01:00,但日志记录从 20:00:59 到 20:01:01(没有提及20:01:00),那么您将什么也看不到。为此,您需要“如果日期大于开始日期”,但这比较棘手。
标签: linux bash shell date scripting