如果找不到时间,应该告诉下一次日志中可用的时间



是否有任何时间函数,可以检查时间,如果找不到的话,请告诉我们下一个可用的日期。

#!/bin/bash
read -p " enter the App name : " app
file="/logs/$app/$app.log"
if ! [ -f "$file" ]; then
    echo "$app.log not found, Please check correct log name in deployer"
    exit
fi
format=$(head -1 $file | awk '{print $1,$2,$3}')
format1=$(tail -1 $file | awk '{print $1,$2,$3}')
read -p " Enter the Date in this Format --'$format' : " first
until grep -q "$first" "$file"
do
  echo "Date not found"
  read -p " Enter the Date in this Format --'$format' : " first
done
final_first=$first
echo "logs are present till this '$format1' date, use this date if you want logs from Provided to latest."
read -p "Enter the end date : " end
until grep -q "$end" "$file"
do
  echo "Date not found"
  read -p "Enter the end date : " end
done
final_end=$end
cd /logs/$app
sed -n " /$final_first/,/$final_end/ "p $file >$app.txt
zip $app.zip $app.txt
rm $app.txt  

输出是

./app_log_extract 
 enter the App name : cspt
 Enter the Date in this Format --'Sep 08 04:53:30' : Sep 08 05
Date not found
 Enter the Date in this Format --'Sep 08 04:53:30' : 

现在,我想和"找不到的日期"一道,日志从9月08 20:09:39

出现。

日志文件是

Sep 08 20:09:39 INFO  [main] common.ComponentManagerImpl - added model class 'com.ge.oilandgas.cspt.model.RoleValidModel' under key 'role-valid'
Sep 08 20:09:39 INFO  [main] common.ComponentManagerImpl - added dispatcher class 'com.ge.casper.servlet.GenericDispatcher' for key 'html'
Sep 08 20:09:39 INFO  [main] servlet.CasperServlet - CASPER is online.
Sep 09 10:57:20 INFO  [http-0.0.0.0-8086-9] model.SessionValidModel - CSPT - User null is logged on!
Sep 09 10:58:49 FATAL [http-0.0.0.0-8086-3] np.dss - UserBeanRepository.query->error in creation of document:

AWK工具主要用于以有用的方式报告一些数据。在这些开始和结束块的情况下,输出将含义更少,此处未定义最终块。调试此代码

./test
 enter the App name : cspt
++ file=/logs/cspt/cspt.log
++ '[' -f /logs/cspt/cspt.log ']'
+++ head -1 /logs/cspt/cspt.log
+++ awk '{print $1,$2,$3}'
++ format='Sep 08 04:53:30'
+++ tail -1 /logs/cspt/cspt.log
+++ awk '{print $1,$2,$3}'
++ format1='at java.lang.Thread.run(Thread.java:662) '
++ read -p ' Enter the Date in this Format --'''Sep 08 04:53:30''' : ' first
 Enter the Date in this Format --'Sep 08 04:53:30' : Sep 08 21:19:19
++ grep -q 'Sep 08 21:19:19' /logs/cspt/cspt.log
++ read month_in date_in time_in
++ awk '
BEGIN {
    split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",month)
    for (i in month) 
        {
        month_nums[month[i]]=i
        echo "$i"    
        }
}
(month_nums[$1] >= month_in) && ($2>=date_in) && ($3>=time_in)
{
   echo "logs are available from    "
    exit 0;   
}
' month_in=Sep date_in=08 time_in=04:53:30 /logs/cspt/cspt.log
++ echo Sep
Sep
++ echo 08
08
++ echo 04:53:30
04:53:30

类似的东西可能会有所帮助:

format="Sep 08 05" # actually, this is swt by user input.
read month_in date_in time_in <<< "$format"
awk '
BEGIN {
    split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",month)
    for (i in month) {
        month_nums[month[i]]=i
    }
}
(month_nums[$1] >= month_nums[month_in]) && ($2>=date_in) && ($3>=time_in){
    print "logs are available from " $1, $2, $3;
    exit 0;   
}
' "month_in=$month_in" "date_in=$date_in" "time_in=$time_in" logfile

最新更新